Search code examples
javascriptandroidreact-nativeexpomqtt

Receive MQTT messages on React-Native Expo


I've tried the following libraries :

react-native-native-mqtt
react-native-mqtt
react_native_mqtt
sp-react-native-mqtt

Most of them gave me the same Error :

null is not an object (evaluating 'Mqtt.createClient')
- node_modules/regenerator-runtime/runtime.js:63:36 in tryCatch
- node_modules/regenerator-runtime/runtime.js:293:29 in invoke
- node_modules/regenerator-runtime/runtime.js:63:36 in tryCatch
- node_modules/regenerator-runtime/runtime.js:154:27 in invoke
- node_modules/regenerator-runtime/runtime.js:189:16 in PromiseImpl$argument_0
- node_modules/react-native/node_modules/promise/setimmediate/core.js:45:6 in tryCallTwo
- node_modules/react-native/node_modules/promise/setimmediate/core.js:200:22 in doResolve
- node_modules/react-native/node_modules/promise/setimmediate/core.js:66:11 in Promise
- node_modules/regenerator-runtime/runtime.js:188:15 in callInvokeWithMethodAndArg
- node_modules/regenerator-runtime/runtime.js:211:38 in enqueue
- node_modules/regenerator-runtime/runtime.js:238:8 in exports.async
.... its pretty long and probably says nothing of value.

The ones that didn't give me this error; gave me errors about sockets and buffers, almost as if it was trying to run it over web sockets.

What am I doing wrong here?

Is it even possible to get MQTT messages on android react-native?

Is Expo the problem?


Solution

  • I had the same problem. An expo Project and fear of ejecting from Expo.

    My actual solution starts from here!

    After a lot of research, I found this module. Eclipse Paho MQTT Client for JS

    npm i paho-mqtt

    Expo Snack Examples:

    https://snack.expo.dev/@rizwanamjad/mqtt

    https://snack.expo.dev/@eseg/mqtt