I am currently making an IoT App that I'm trying to connect to a Raspberry Pi using MQTT. I use the react_native_mqtt package. The problem I have is that it doesn't connect. What I'm trying to achieve is to receive data from the rasp and use that with react native. But the connection doesn't work. Any help is appreciated.

Error: Object { "errorCode": 7, "errorMessage": "AMQJS0007E Socket error:undefined.", "invocationContext": undefined, }

import init from 'react_native_mqtt'
import AsyncStorage from '@react-native-async-storage/async-storage'

  size: 10000,
  storageBackend: AsyncStorage,
  defaultExpires: 1000 * 3600 * 24,
  enableCache: true,
  reconnect: true,
  sync : {

const host = ''
const port = '8883'
const connectUrl = `mqtt://${host}:${port}`
clientID = "clientID-" + parseInt(Math.random() * 100);
export default class TestScreen extends Component {

    this.onConnectionLost = this.onConnectionLost.bind(this)
    this.onConnect = this.onConnect.bind(this)
    const client = new Paho.MQTT.Client(host, Number(port), clientID);
    client.onConnectionLost = this.onConnectionLost;
      onSuccess: this.onConnect,
      useSSL: true,
      userName: 'admin',
      password: 'admin',
      onFailure: (e) => {console.log("here is the error" , e); }


    this.state = {
      message: [''],
      isConnected: false,
  onConnect = () => {
   // const { client } = this.state;
    this.setState({isConnected: true, error: ''})


const mqtt = require('mqtt')
const host = '52.xx.xx.xx'
const port = '1883'
const clientId = `id_${Math.random().toString(16).slice(3)}`
const connectUrl = `mqtt://${host}:${port}`
const client = mqtt.connect(connectUrl, {
  clean: true,
  connectTimeout: 4000,
  username: 'xxx',
  password: 'xxx',
  reconnectPeriod: 1000,
module.exports = client;

const topic = 'EnergyMonitoring/energy'

client.on('connect', () => {
  client.subscribe([topic], () => {
    console.log(`Subscribe to topic '${topic}'`)

client.on('message', (topic, payload) => {
  console.log('Received Message :', topic, payload.toString())

app.listen(1883, ()=>{
  console.log("server is running")
  • 1
    Can you connect with any other MQTT client? Assuming the IP address in the code is correct it looks like the ip/port is firewalled off as the connection times out.
    – hardillb
    Nov 29, 2021 at 17:38
  • 1
    If you can't connect to that broker with any client, then seeing the client code isn't useful, you need to be looking at the broker (and any firewalls that may between you and the broker)
    – hardillb
    Nov 30, 2021 at 9:48
  • 2
    That last comment directly contradicts the previous 2 answers when I asked if you could connect with a different client. Edit the question to show both the broker config and EXACTLY how you managed to connect with NodeJS/Python.
    – hardillb
    Nov 30, 2021 at 10:10
  • 1
    Explain why you have changed the port number between the NodeJS and the react native? You have also ignored my request for details of how the broker is configured.
    – hardillb
    Nov 30, 2021 at 10:22
  • 1
    Let us continue this discussion in chat.
    – Kindth
    Nov 30, 2021 at 10:30

1 Answer 1


We solved this by edit the Mosquitto config file to either add a new listener port 8883 and to use the WebSocket protocol for that port.

From Stack Overflow: Seting mosquitto broker to listen on two ports?

It was a config problem. Thanks @hardillb for the helping and at @kalyanswaroop for the replies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.