-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
React-Native support #573
Comments
Other easy solution is to add a postinstall hook that creates the build every time someone installs the package |
Hey @franleplant, I understood your solution but I did not understand the problem you are facing. You can easily do |
Wow, apparently I'm blind, I need 👓 . I did found however this in the gitignore https://github.com/mqttjs/MQTT.js/blob/master/.gitignore#L10 which might have confused me. So, two more things before closing
This is the way Im importing it
The mqtt dist are made with browserify and my project uses webpack and react native, there might be some incompatibilities with the module systems? Thanks a lot for your help |
@franleplant that is highly possible. You probably have to have your webpack build it together with your application |
So, after all, Im not using webpack, React Native uses it's own build system (analogue to Webpack and Browserify): Packager Some more relevant information about other's peoples problems: facebook/react-native#1871 Potential solution I've tested is ReactNativify but in the end it didn't work with TLDR |
@franleplant In the end what was your solution for MQTT on React Native? |
hey @keyeh so after a lot of battling I ended up using https://github.com/rh389/react-native-paho-mqtt It's rather immature and I've already had submit a patch but it's all that we got. Kudos to the author though, very responsive and nice to work with. I'd love to use mqtt.js but I didn't find a way to easily make it work with React Native Packager. Paho let's you use whatever store that implements an interface as a replacement for LocalStorage. I hope this helps. |
I'm going to be working on React-Native support within the next two weeks, I had an initial attempt that let me compile the module successfully, but it resulted in runtime errors. I'll post here when I have progress. |
@RangerMauve let me know if there is something I can take a look, give you feedback, give you PRs, because Im right now working on an RN App that uses mqtt, so I can live test it. |
@franleplant Thanks for the offer! Here's the PR I did to get the ball rolling and discussion around it: nodejs/readable-stream#258 The original problem was that I added a fix to explicitly exclude the My idea (when I get around it it, or somebody else does), is to add a check so that if Stream is required properly, we should add a check to see if it actually has some stream-related property defined before using it. Something like var Stream;
(function () {
try {
Stream = require('st' + 'ream');
} catch (_) {} finally {
if (!Stream) Stream = require('events').EventEmitter;
}
// This is the addiition
if(!Stream.on) Stream = require('events').EventEmitter;
})(); |
You can try modifying the file in your node_modules folder to see if it works and if there's other issues that pop up. |
Cool, Im going to investigate a bit next week, if I can make my self some time. Thanks a lot for your participation! |
Well, I'm going to get it to work by the end of April for sure since it's relevant to my day job. We've just been focusing on a |
Sorry for using this thread as a forum, feel free to kick me out but this info is interesting for mqtt and react native and the community and does not require any effort from the mqtt.js maintainers. So, I tried to make mqtt.js work with React native and went through a bunch of hoops but I think I made some progress, Im stuck in a place right now which if I understand how to get out I will update this post with it, but perhaps someone can help me with this. These are the steps that I took to get to the point where I am now.
In your package.json add these to your deps
These point to today's master's top of each repo, you can update the commit hash accordingly. This step basically says to your dependency tree (i.e. mqtt.js dependencies) that when doing
Make sure it looks something like this import React from 'react';
import { AppRegistry } from 'react-native';
// Define globally required stuff
GLOBAL.Buffer = require('buffer').Buffer;
// Some dependencies require process to be defined, so we comply with that
GLOBAL.process = {
browser: true,
env: {
NODE_ENV: __DEV__ ? 'development' : 'production',
}
}
// Make sure to use require instead of import syntax since the former
// will respect the order of things
const App = require('./app');
AppRegistry.registerComponent('MyApp', () => App);
Get stuck with this error:
Which basically refers to this file node_modules/websocket-stream/server.js var inherits = require('inherits')
// Server is undefined, and that is the problem
var WebSocketServer = require('ws').Server
var stream = require('./stream')
module.exports = Server
function Server(opts, cb) {
if (!(this instanceof Server)) {
return new Server(opts, cb)
}
WebSocketServer.call(this, opts)
var proxied = false
this.on('newListener', function(event) {
if (!proxied && event === 'stream') {
proxied = true
this.on('connection', function(conn) {
this.emit('stream', stream(conn, opts))
})
}
})
if (cb) {
this.on('stream', cb)
}
}
// In here, inherits tries to access WebSocketServer.prototype to
// do the inheritance but WebSocketServer is undefined
inherits(Server, WebSocketServer) Im going to try to keep bashing at this, but if anyone has some insights they will be highly appreciated. @RangerMauve probably this could be a very simple approach for resolving the mqtt.js in React Native problem |
That error is kind of weird. It shouldn't be requiring that file and instead should be getting Also, are you using the latest version of MQTT.js? Also, what version of React-Native are you using for bundling? |
OSX The file looks pretty unconditional to the operative system or anything for that matter |
Are you 100% sure that the error is within that file and not in here? Can you post a stack trace? |
100% sure because Im not doing anything magical, Im just following the stack trace. The first item is the Make sure you follow my steps to see in you can replicate this problem. The problem with the stream might have been solved by steps 3 or 5. |
What version of MQTT.js and websocket-stream are you seeing in your package.json? That file shouldn't even be imported because websocket stream specifies a browser field in it's package.json which should prevent it from loading It could be that either websocket-stream or packager are out of date if that alias isn't being effected. Edit: Not package.json, node_modules folder |
Greped from my yarn lock That's weird, Im going to try to update my react native cli Also, I think I saw that one of the problems with packager is that it doesnt recursively apply the "browser" field resolve, I need to find the issue where they disscuss about it though |
My react native version is the latest: react-native --version I would recommend to follow my steps to see where you get to |
Hmm, sadly I don't have time today since I've gotta work on other stuff, but as I said, I'll be getting to it in about a week. In the meantime, there's definitely something fishy happening with the packager not respecting the AFAIK even though the CLI is installed globally, the scripts defined in your project for |
I've been dealing with the "property of undefined" issue for the last week. My findings on this is that it is a problem with React Native's packager not using correctly the This only happens when you are trying to override the
works fine with webpack, but with React Native, I made a test where you would create an extra hop to avoid using the index.js
package.json
This worked for me, but it is just a hack. The real problem seems to be the packager on React Native is not correctly replacing |
@alexleonescalera Good find! You should open an issue in their repo so that somebody looks into it. :D |
good work @alexleonescalera, just to double check, by adding this |
@RangerMauve Can we use this solution (or other) to use mqtt://rabbitmqBroker to send messages to RabbitMQ broker, through react-native app? Or it only works with use of websockets(ws://rabbitmqBroker)? |
@angelos3lex I haven't used a non-websocket broker with this technique yet, so I can't say for sure, but you might be able to get it to work by using the react-native-tcp library and aliasing Let me know if you get it to work or have any trouble setting it up. |
install
edit node_modules/mqtt/package.json
app.js
|
It works in Android , |
Try using Websockets instead of TCP |
Also, rn-nodeify might help with getting modules to work properly. |
i used rn-nodeify but got error
Code
shim.js
script
|
There seems to be a weird character in your strings, is that on purpose? |
@RangerMauve Thanks! i fixed that issue. but Client doesn't connect and no error is emitted |
You're sure you got the right host/port and that your broker supports websockets? |
@RangerMauve yes, you can try it |
Exact same problem, anyone has an update? |
@vasupol11 @RangerMauve it worked with "react-native-mqtt-client" and "rn-nodeify"
Example: import connect from 'react-native-mqtt-client'
export default async (clientId) => {
// const clientId = await deviceInfo.getUniqueID()
return eventChannel(emitter => {
try {
if (socketInstance !== null) {
socketInstance.end()
socketInstance = null
}
const options = {
...DEFAULT_MESSAGING_OPTION,
clientId
}
console.show('MQTT Connect', MESSAGING_URL)
socketInstance = connect(MESSAGING_URL, options)
socketInstance.on('connect', () => {
console.show('MQTT Connected', MESSAGING_URL)
emitter({ type: MESSAGES.CONNECTED, url: MESSAGING_URL })
})
socketInstance.on('message', (topic, message) => {
console.show(`Message Topic ${topic}`, topic, message.toString())
try {
const data = JSON.parse(message.toString())
emitter({ type: MESSAGES.EVENT, topic, message: data, success: true })
if (listeners[topic] && typeof listeners[topic] === 'function') {
listeners[topic](data, topic)
}
} catch (err) {
}
})
} catch (err) {
console.problem(err)
}
return () => {
if (socketInstance !== null) {
socketInstance.end()
socketInstance = null
}
}
})
} |
@zrg-team That's great to hear, thank you for sharing! :D |
I'm afraid it doesn't work. @zrg-team @RangerMauve . |
https://www.npmjs.com/package/@taoqf/react-native-mqtt |
Any update on this issue? |
This works. FINALLY 🎉🎉 |
Another solution (for react-native apps with typescript): // @types/mqtt.d.ts
declare module 'mqtt/dist/mqtt' {
export * from 'mqtt';
} // screen.ts
import mqtt from 'mqtt/dist/mqtt'; |
I tried to use this to connect to ali mqtt, but it kept failing, but I upgraded mqttjs to the latest version, and the connection was normal if I directly referenced mqttjs. |
This worked well 🙌, but it's a bit behind the upstream version of mqtt at this point. So I forked the latest mqtt version and added what seems to be the minimal amount of changes needed to have it running in React Native: #1574 (published as |
Hi Guys, I want to connect MQTT broker that will be on engin8 raspberry pi 4 mqtt://username:password@host:1883 with React Native. I tried Paho-mqtt and did not work out. Please Help me . |
Hi everyone, I started helping maintaining this library. |
I have fixed Browser docs by adding webpack and vite setup. Check them out |
Hi There!
Thanks a lot for your work on this amazing library.
I've been fighting a lot with the browser builds, for several reasons:
rm -rf node_modules
, which is too oftenSo, that's my problem, and here is my solution, which aims to be as simple as it gets
dist/mqtt.js
to the source files, so I can easilyrequire(mqtt/dist/mqtt)
. Names can be discussed but the important thing is to have that file availableThere are other variations of this solution but I think this works and it's super simple and you already got the script to generate those dist files, the only remaining thing to do would be to un-ignore
dist
and to fix all the pre-commit checks that fail on those dist filesWhat do you think?
There might be more fancy solutions and Im open to anything but this requires minimal work and provides maximum satisfaction
The text was updated successfully, but these errors were encountered: