I am trying to make a connection with thingsboard by subscribing to a topic so that I can receive an updated state changes. But after subscription the client seems to be disconnected. Below is my code
const mqttEndpoint = 'MyLocalIpAddress:1883'
const MQTT_TOPIC = 'v1/devices/me/attributes'
const mqttClient = mqtt.connect(`mqtt://${mqttEndpoint}`, {
username: '123456789asdf',
rejectUnauthorized: false,
})
mqttClient.on('connect', function () {
mqttClient.subscribe(MQTT_TOPIC, function (err) {
if (!err) {
console.log('subscribed')
}
})
})
mqttClient.on('message', (topic, message) => {
// message is Buffer
console.log('topic', topic);
console.log('message', message.toString());
//this.server.emit('msgToClient', message.toString());
mqttClient.end()
})
mqttClient.on('close', () => {
console.log('mqtt: connection closed');
})
I think the javascript mqtt code seems to be ok. But I am not figuring it out how can receive an update state from the thingsboard via mqtt.
The events in the console while running the above code:
Processing connect msg for client: mqttjs_f7181153!
Processing connect msg for client with user name: 123456789asdf!
Client connected!
subscribed
Client disconnected!
Also I had tried it with mosquitto but seems to be the connection is successful but not receiving anything if there is an update in state
mosquitto_sub -d -h "localhost" -t "v1/devices/me/attributes" -u "123456789asdf"