Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

React Native Support#1841

MaximoLiberata started this conversation inGeneral
Apr 12, 2024· 4 comments· 17 replies
Discussion options

In the version5.5.2 was fixed an issue that didn't allow use MQTT.js in React Native (RN). The motivation of this discussion is bring a example of how to implement MQTT.js in React Native and also maybe bring ideas of enhancements for RN environment.

You can see the example inReact Native MQTT.js Implementation

You must be logged in to vote

Replies: 4 comments 17 replies

Comment options

@MaximoLiberata Thanks! Could you kindly add a link to this on readme please?

You must be logged in to vote
1 reply
@MaximoLiberata
Comment options

Hi@robertsLando, thanks to you!

I opened the PR#1842

Comment options

Wondering if this is still needed?https://github.com/MaximoLiberata/react-native-mqtt.js-example/blob/main/App.js#L8

You must be logged in to vote
11 replies
@robertsLando
Comment options

Just to be sure, is react native using the browser bundle? If so I think this could be easily fixed by patching the esbuild bundle here:https://github.com/mqttjs/MQTT.js/blob/main/esbuild.js

Maybe by adding a new entry on "define" or manually adding the polifilly on top of the output bundfle, could you guys do some tries?

@MaximoLiberata
Comment options

I tried to use the browser bundle, but they threw a exeption:

import * as mqtt from 'mqtt/dist/mqtt' → Return {"default": {}}
import * as mqtt from 'mqtt/dist/mqtt.min' → Return {"default": {}}
import mqtt from 'mqtt/dist/mqtt.esm' → Throw Error: ws does not work in the browser. Browser clients must use the native WebSocket object

React Native doesn't use the browser bundle, I think its behavior is closer to node.js than browser.

Also, I tried to add a piece of code in thesrc/index.ts file and worked:

import { isReactNativeBrowser } from './lib/is-browser'import * as mqtt from './mqtt'if (isReactNativeBrowser && typeof process?.nextTick !== 'function') {process.nextTick = (callback: () => void) => {setTimeout(callback, 0)}}export default mqttexport * from './mqtt'

Not is the best solution, this change also modify theprocess object in the global scope.

@abdelhameedhamdy
Comment options

@MaximoLiberata

I just tried adding this block

if (typeof process?.nextTick !== 'function') {process.nextTick = setImmediate;}

or, same like

if (typeof process?.nextTick !== 'function') {process.nextTick = (callback: () => void) => {setTimeout(callback, 0)}}

inhttps://github.com/mqttjs/MQTT.js/blob/main/src/lib/connect/index.ts#L11

I think it fixed it, without needs to add it at top of RN App.

If you can have a look to check if that might be a proper solution.

@abdelhameedhamdy
Comment options

@MaximoLiberata Did the last proposed solution fix the issue!

@MaximoLiberata
Comment options

Yes, I tested in the RN APP example and it worked well.

Comment options

Good, I think this update might work with releasev5.5.1 as a fix, I am not sure, needs to best tested.
If it did not, so v5.5.2 needs to add this update with a new PR in your current RN mqtt.js fork.

You must be logged in to vote
0 replies
Comment options

I tried to use the browser bundle, but they threw a exeption:

Nope what I meant is if react is actually using the browser bundle or not, the bundle is automatically served based on the package.jsonexports field if it detects a browser env, otherwise the "node" version is serverd.

I think that adding:

if (isReactNativeBrowser && typeof process?.nextTick !== 'function') {process.nextTick = (callback: () => void) => {setTimeout(callback, 0)}}

to the connect/index.ts isn't so bad, someone wants to submit a PR?

You must be logged in to vote
5 replies
@abdelhameedhamdy
Comment options

@robertsLando
I just tested it vsv5.5.1, it works well with just updating connect/index.ts, and only the first commit by@MaximoLiberata inBufferedDuplex.ts
which needs importing Buffer package.

So, I am not sure that other commits in (fc8fafb) are required, as no error appears for me at least. may be required for other issue.

just this block in connect/index.ts, and importing Buffer

if (typeof process?.nextTick !== 'function') {process.nextTick = setImmediate;}

even no need for importing isReactNativeBrowser() in connect/index.ts

with regards to using setImmediate or setTimeout(callback, 0)
I searched for the difference between both, and it seems that it ispreferred to use setImmediate.

@abdelhameedhamdy
Comment options

Just to update, my issue was that process.nextTick is not a function was blocking my RN App to connect to the broker.
(fc8fafb) was solving a different issue other than mine, so the new proposed fix for resolving the issue to add a block of code at start of RN App to let it work.

So, just a PR is required to fix the process.nextTick is not a function error for RN Apps.

@abdelhameedhamdy
Comment options

Another notice here, I did not get this error: Cannot create URL for blob (#1840) in my RN App; as I am setting timerVariant: "native" not "auto".

@robertsLando
Comment options

Another notice here, I did not get this error: Cannot create URL for blob (#1840) in my RN App; as I am setting timerVariant: "native" not "auto".

Yeah that's the solution I suggested at start in the issue

@abdelhameedhamdy
Comment options

@robertsLando I just opened PR#1849.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
3 participants
@MaximoLiberata@robertsLando@abdelhameedhamdy

[8]ページ先頭

©2009-2025 Movatter.jp