Paul Caponetti
Paul Caponetti

Reputation: 325

MQTT on React Native?

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?

Upvotes: 15

Views: 18322

Answers (3)

SudoPlz
SudoPlz

Reputation: 23313

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.

Upvotes: 8

aestrro
aestrro

Reputation: 993

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!

Upvotes: 6

Igor Makarov
Igor Makarov

Reputation: 729

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.

Upvotes: 5

Related Questions