I am trying to connect to an Azure IoT Hub directly using MQTT to publish message.
For that I have tried multiple packages but I am not able to connect successfully.
react-native-mqtt-clientssp-react-native-mqtt mqtt @ko-developerhong/react-native-mqtt But not working.
Here is one of code reference using sp-react-native-mqtt.
export const connectToMQTT = async (iotHubName, deviceId, sasToken) => {
const user = `${iotHubName}/${deviceId}/?api-version=2021-04-12`;
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);
});
};
Any input will be helpful thank you
New contributor
Coder87 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.