In this tutorial, we are going to learn all about setting up a private Mosca MQTT broker using Node.js. But before we get into it let us brush up what we know about MQTT protocol. MQTT stands for Message Queuing Telemetry Transport. It works on the pub sub-model. The broker stands between the publisher and the subscriber, relaying messages from publisher to subscriber.
We will be making the system that we are using the broker using the npm’s Mosca and Node.js. I will be doing it for a windows based system but it can similarly implemented for different operating systems.
First thing is to install the necessary packages that we would require. To do the same follow the following steps:
#cmd inside project directory
npm init
npm install mosca --save
A word of caution just make sure that node modules installed in your project directory. And you will also need to have mosca and mqtt packages available globally in other words in your project folder.
#broker.js
var mosca = require('mosca');
var settings = {
port:1883
}
var server = new mosca.Server(settings);
server.on('start', function(){
console.log("start");
});
The above code will be setting up the broker. Here is a brief simplified explanation of what the code does.
The code configures our broker(server) to use the port 1883 for communication. This is passed as a parameter to JS object named ‘settings‘ which is then passed to Server function.
It is then turned on by an event ‘start’ and calls back an anonymous function. And whenever the server is up and running the console log gets the message ready.
The broker alone cannot do anything, so let us setup a publisher and a subscriber so that we have a complete system in place and we can test is as well.
Setting up the publisher
Here we would be configuring the publisher using the same packages and this will setup a publisher working in your system and it is not for connecting some other embedded device as a publisher. This will just enable you to check and verify is your broker is working fine or not. So, let’s get started.
#publisher.js
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://Broker ip address(in this case the pc's ip address');
client.on('connect', function () {
setInterval(function() {
client.publish('NameOfTopic', 'Hello mqtt');
console.log('Message Sent');
}, 5000);
});
The code first gets the necessary mqtt packages. It then creates a client variable to which ip address or say broker’s address is passed on as a parameter. Whenever the event connect is encountered the anonymous function gets called which publishes message to the topic.
#subscriber.js
var mqtt = require('mqtt')
var client = mqtt.connect('mqtt://your_ip_address_of_pc')
client.on('connect', function () {
client.subscribe('NameOfTopic')
})
client.on('message', function (topic, message) {
context = message.toString();
console.log(context)
})
Similarly, we have got a subscriber running through the above code. The working of code is similar to what we have discussed above, just to clear the air we are sending message and it is stored in form of bits so the bits individually don’t make any sense they are stored in a buffer. So, they are converted from the raw form to a string that makes sense to us using the ‘.toString ‘ function.
To conclude this will configure your system as a private Mosca MQTT broker using Node.Js and by configuring a publisher and subscriber you can have a complete IoT setup in place.