MQTT on React Native?

Paul Caponetti picture Paul Caponetti · Mar 2, 2016 · Viewed 10.2k times · Source

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?

Answer

aestrro picture aestrro · Aug 24, 2016

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!