Connect to MQTT client for IOT hub with react native

Shahanshah Alam 0 Reputation points
2024-06-24T07:41:52.23+00:00

I am trying to register and provision device to upload file to IOT hub using rest api.
In the process, after registering the device using registerapi and checking the operation status using operationStatusApi.
I need to connect with MQTT client to publish telemetry message.
I am trying to connect with MQTT, but not getting success.
I have tried so far multiple package which support mqtt integration and can connect with

mqtt://test.mosquitto.org:1883

but when I am trying azure it gets failed.
I have tried sp-react-native-mqtt , mqtt, @ko-developerhong/react-native-mqtt

export const connectToMQTT = async (iotHubName, deviceId, sasToken) => {
  const user = `${iotHubName}/${deviceId}/?api-version=2021-04-12`;
  console.log('mqtt url', iotHubName);
  console.log('mqtt deviceId', deviceId);
  console.log('mqtt sasToken', sasToken);
  console.log('mqtt user', user);
  MQTT.createClient({
    // uri: `mqtts://${iotHubName}:8883`,
    clientId: deviceId,
    username: user,
    pass: sasToken,
    protocol: 'mqtts',
    host: iotHubName,
    port: 8883,
    keepalive: 60,
  })
    .then(function (client) {
      client.on('closed', function () {
        console.log('mqtt.event.closed');
      });

      client.on('error', function (msg) {
        console.log('mqtt.event.error', msg);
      });

      client.on('message', function (msg) {
        console.log('mqtt.event.message', msg);
      });

      client.on('connect', function () {
        console.log('connected');
        client.subscribe('/data', 0);
        client.publish('/data', 'test', 0, false);
      });

      client.connect();
    })
    .catch(function (err) {
      console.log(err);
    });
};

this is one of the code

but getting error
connection failure Connection lost (32109) - java.io.EOFException

Please help me with this.
Thank you

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,153 questions
0 comments No comments
{count} votes