, ,

MQTT using Node JS | Node JS Tutorial

This post shows you Simple MQTT Application using Node JS. There are three parts of the application.
For running any MQTT application, we need a 

1. Broker (The broker can be anywhere in the Internet or cloud or a local machine) 
2. Publisher (pub.js is the name of the file)
3. Subscriber (sub.js is the name of the file)

Check the following video for complete instructions.




In this example, we will be using a broker called https://www.hivemq.com

To use the cloud broker (mqtt://broker.hivemq.com) and connect the publisher and subscriber with a topic and a message. To install the Hive MQ software in your local machine (127.0.0.1) and run the same above code locally and make the connectivity between the pub-sub.

  • We open two new .js files called pub.js (publisher) and sub.js (subscriber) in Visual Studio Code. To install the mqtt module in NodeJS, we use the command “npm install mqtt” in C:/ terminal. 
  • We define our client as connecting to “mqtt://broker.hivemq.com”. We connect to the cloud broker in both publisher and subscriber js modules. We have taken temperature and temperature value as our message. The source code is given below. 
  • To execute our code, we open two terminals and use the command “node pub.js” and then in the other one, “node sub.js”. In the left command prompt, we have published, and in the one on the right, we have subscribed. We can see the outputs match each other. 
MQTT using Node JS
MQTT using Node JS

Using cloud broker:

Pub.js:

const mqtt = require("mqtt");

var client = mqtt.connect("mqtt://broker.hivemq.com");


client.on("connect",function()

{

    setInterval(function(){

        var random = Math.random()*50;

        console.log(random);

        if(random<30)

        {

            client.publish("Pradeep","temperature value: "+random.toString());

        }

    }),30000;

});


Sub.js:


const mqtt = require("mqtt");

var client = mqtt.connect("mqtt://broker.hivemq.com");


client.on("connect",function()

{

    client.subscribe("Shirish");

    console.log("Client subscribed ");

});


client.on("message",function(topic, message){

    console.log(message.toString());

});



Using local broker:

  • To run the broker locally, we first install HiveMQ on our system.
HiveMQ for MQTT
HiveMQ for MQTT

Pub.js:


const mqtt = require("mqtt");

var client = mqtt.connect("mqtt://127.0.0.1:1883");


client.on("connect",function()

{

    setInterval(function(){

        var random = Math.random()*50;

        console.log(random);

        if(random<30)

        {

            client.publish("Shirish","temperature value: "+random.toString());

        }

    }),30000;

});



Sub.js:


const mqtt = require("mqtt");

var client = mqtt.connect("mqtt://127.0.0.1:1883");


client.on("connect",function()

{

    setInterval(function(){

        var random = Math.random()*50;

        console.log(random);

        if(random<30)

        {

            client.publish("Shirish","temperature value: "+random.toString());

        }

    }),30000;

});



2 comments:

  1. Please how do I create an API for the mqtt publish in node.js

    ReplyDelete
  2. Thoda dekh ke dala karo sir niche wale mai pub or sub mai same data hai

    ReplyDelete