-
MQTT ---> Message Queuing Telemetry Transport
-
it is a client library for MQTT protocol
written in JavaScript
for node.js and the browser -
MQTT is a lightweight client-server publish/subscribe messaging protocol
-
MQTT is used for data exchange between devices and server applications
npm install mqtt --save
- Publisher
- broker --- inside broker there are some queues
- subscriber
- connect
client.on('connect', function (connack) {
console.log('Connected')
})
- reconnect
client.on('reconnect', function () {
console.log('Reconnecting...')
})
- close
client.on('close', function () {
console.log('Disconnected')
})
- disconnect
client.on('disconnect', function (packet) {
console.log(packet)
})
- publish
Client.publish(topic, message, [options], [callback]
🔽
client.publish('topic', 'Hello world')
🔽
client.publish('testtopic', 'Hello, function () {
console.log('Published')
})
- subscribe
client.subscribe('test', function (err) {
console.log(error)
})
- error
client.on('error', function (error) {
console.log(error)
})
- message
client.on('message', function (topic, payload, packet) {
console.log(`Topic: ${topic}, Message: ${payload.toString()}, QoS: ${packet.qos}`)
})