Using Node.js MQTT Library - Part 1

You might have heard about the buzz word “IOT” or Internet of things. Yes, MQTT is the most commonly used protocol in internet of things. There are two key things one has to understand i.e. the publisher and the subscriber. Let us see some background about publisher and subscriber.

Subscriber is the one which is interesting in receiving messages which are published by publisher. You can have one or more subscribers registered to receive messages. Each of these client gets subscribed to messages via topic or topics.

Publisher is the one, which is responsible for publishing the messages. All clients which are subscribed to a specific topic or topics will get notified with the message published by the publisher.

We are building a sample “nodejs” client and server application using MQTT library to demonstrate how we can take advantage MQTT nodejs library is subscribing and publishing messages with ease. Node.js is very powerful, simple and easy to use. One can really take advantage of building simple and elegant applications using “nodejs”.

First, before we start working on the sample application, there are couple of things we have to do. Yes, that’s installing “MQTT” node.js library and some of the dependencies.

Setting up the dependencies and required node.js libraries

Step 1:
Download and install Python 2.7.10

Step 2:
Execute the following command in command prompt.

npm config set python C:\Python\python.exe

Step 3:
Install MQTT node.js library by executing the following command.

npm install –g mqtt

receiving message

Demo App


Here’s the scenario for which we are coding the node.js application.

For demonstration purpose, we are making use of the following JSON weather forecast sample.
Sample forecast.

MQTT Node.js Client


The following is the code snippet for MQTT client, where you can see the client is getting subscribed to a specific topic – “mydevice/forecast”. The handleMessage is something which gets called asynchronously when the client receive message from the publisher.

  1. var mqtt = require('mqtt')  
  2. var broker = 'mqtt://test.mosquitto.org';  
  3. var client = mqtt.connect(broker);  
  4.   
  5. client.subscribe('mydevice/forecast');  
  6. client.handleMessage = function(packet, done) {  
  7.    console.log(packet.payload.toString());  
  8.    done();  
  9. }  
In order to run the client. Just execute the client.js. You should see the message shortly after running the server code which actually publishes the message.

The following is the snapshot of the client before receiving message.

message

After receiving published message from server.

run the server program

MQTT Nodejs Server code

The following is the code snippet of MQTT Server program. We need to import ‘mqtt’ library and then create a client instance so that it can publish messages. We are going to read a “JSON” file named ‘forecast.json’ and send all the contents as part of the message that’s getting published.

Notice below, the publisher has to use the same broker and topic name in order to publish messages to subscribed clients.
  1. var mqtt = require('mqtt')  
  2. var fs = require('fs')  
  3. var broker = 'mqtt://test.mosquitto.org'  
  4. var client = mqtt.connect(broker)  
  5. var forecast = fs.readFileSync('../forecast.json').toString();  
  6.   
  7. client.publish('mydevice/forecast', forecast)  
  8. client.end();  
  9.   
  10. console.log('Successful!');  
In order to run the server program, just key in “node server.js” and hit enter. The following is the snapshot of the program running on node.js.

program running

Key thing about MQTT broker

You might have noticed the MQTT client is connecting with the following url. It’s a public broker. It’s called Mosquitto MQTT server/broker. You can find more information on http://test.mosquitto.org// .There’s a provision to set up our own MQTT server and make use of it too.

Next Recommended Readings