MQTT on React Native?

狂风中的少年 提交于 2019-12-04 10:50:46

问题


I'm having a hard time figuring out the right way to go when adding MQTT to a react-native project I have. The project needs to run on iOS and Android, so ideally the MQTT can be handled in the javascript side. I realize the networking lair is different from mobile to classic node, so I began down the route of forking MQTT.js and have hit roadblock after roadblock.

Should I continue down the route of forking MQTT.js? Should I aim to replicate the node environment on the mobile environment with polyfills or some other means? Should I break down and get an objective C library and a Java library and wrap them?


回答1:


I've had moderate success using react-native-mqtt both on Android and iOS. It simply bridges over native modules per each platform, Paho on Android and MQTTClient on iOS.




回答2:


After really needing the react-native-mqtt to work and failing to configure it properly, alas, I have found react_native_mqtt. Yes, they look the same but trust me, they're different.

In this module you won't have to struggle with Podfiles (ios) or MainActivity.java or MainApplication.java (android) files to get this working. All you will need to do is include it.

and follow the instructions:

import { initMqtt } from 'react_native_mqtt';
import { AsyncStorage } from 'react-native';

... your awesome code below!



回答3:


I did some research and it looks like the way to go is with native (tcp) mqtt, and not mqtt over websockets (eventhough you could do both).

The road was unpaved, so after looking into all of our options, I ended up modifying tuanpmt/react-native-mqtt in order to make the library more stable, and are now using it in production.

There you go: https://github.com/SudoPlz/sp-react-native-mqtt

That's native mqtt (using iOS and Android mqtt libraries) and not a websockets js library that needs weird configuration and imports to work with react-native. I'd say that's your best bet right now.

npm install sp-react-native-mqtt and then follow the steps in readme to use it.

Try it.



来源:https://stackoverflow.com/questions/35753636/mqtt-on-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!