MQTT With NodeJS

Posted By : Himanshu Goyal | 12-May-2016

MQTT NodeJS

MQTT: It stand for Message Queue Telemetry Transport.This is light weight TCP/IP protocol for transfer the message . To acheive the mqtt message we will use third party borker ie.HiveMQ, IBM MessageSight, JoramMQ, Mosquitto, RabbitMQ, Solace Message Routers, and VerneMQ etc.We create two channels one is publish and second is subscribe. Sender  publish the message on pub channel and receiver subscribe the sub channel to get the message.

MQTT Methods: By default mqtt provide the five methods 

  1. Connect:This is used to make the connection with third party mqtt broker.
  2. Disconnect:This is used to disconnect the tcp/ip connection with mqtt broker when mqtt client is not used.
  3. Subscribe:This is used to subscribe the channel to receive  the messages.
  4. Publish:This is used to publish the channel to publish the messages.
  5. Unsubscribe:This is used to unsubscribe the one or more topics which subscribe by mqtt client/

MQTT with NODEJS:

  • npm install mqtt --save //to install the mqtt npm module
  • var mqtt    = require('mqtt'); //include the dependancy
  • var client  = mqtt.connect('url of the mqtt broker'); //make the client connection with third party mqtt broker
  • client.on('connect', function () {} //when client is connect with mqtt broker the event is fired
  • client.subscribe('topic'); // client is subscribe the topic to receive the messages.
  • client.publish('topic'); //client is publish the topic to send the messages .
  • client.on('message',function(topic,message){}); //event is fire when any message is sent to mqtt broker.?

Procedure to Store history of Mqtt:

  • When Mqtt client message event fire then you get the full data in message.
  • You should have pub-client-id,sub-client-id,message,message type etc as you want .These all information is used to maintain the history of chatting.
  • Then parse these all information String to Json becasue mqtt will be send all information to mqtt broker in form of string.
  • Find the database record by using unique pub/sub channels .
  • Then implement the bucket concept to store all the messsage in database 

 

Thanks

About Author

Author Image
Himanshu Goyal

Himanshu is a bright Java ,Mean stack developer and have good knowledge of Hibernate, Spring,FFmepg,Neo4j JSON, Jquery, NODE.JS .His hobbies are learning new things, fitness .

Request for Proposal

Name is required

Comment is required

Sending message..