MQTT on React Native?

徘徊边缘 提交于 2019-12-03 06:27:07

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.

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!

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.

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