Connect to MQTT client for IOT hub with react native

Shahanshah Alam 0 Reputation points

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


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);
    // 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 () {

      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 () {
        client.subscribe('/data', 0);
        client.publish('/data', 'test', 0, false);

    .catch(function (err) {

this is one of the code

but getting error
connection failure Connection lost (32109) -

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