Setting up private MQTT broker using Mosca in Node.js

Alif Abdullah
Jan 10, 2018 · 3 min read

Preface

Hey everyone, I welcome you all to my new tutorial on how you can set up a very basic private MQTT broker in windows PC using Node.js. The prerequisite for this lesson is you should at least be familiar with MQTT basics and Node.js. Node must be installed on your computer.

While a public broker can be vulnerable to your important data or is often loaded with massive internet traffic, you can easily create your own MQTT broker in your PC. Currently, I am using Windows 10 and I am going to demonstrate how you can create a basic private MQTT broker using Node.js and npm’s Mosca API inside of your computer.

Kernel

You must have node modules installed in your project directory. You also have to require mosca and mqtt packages globally or inside of your project folder.

#cmd inside project directory
npm init
npm install mosca --save

which will results in creating node modules folder and package.json file inside the project directory.

Just to get a glimpse of MQTT basics, a MQTT system is made of with a broker, subscribers, and publishers. So, we got to build a broker first which will relay the messages between the publishers and subscribers.

#broker.jsvar mosca = require('mosca');
var settings = {
port:1883
}

var server = new mosca.Server(settings);

server.on('ready', function(){
console.log("ready");
});

You have to tell Mosca which port it’s going to occupy i.e. the default port 1883 is given here as a JavaScript object named settings, and then pass this object inside the Server function to get it ready to work. Finally, the mosca server, aka, the broker is turned on with an event “ready”, followed by an anonymous callback function.

A broker is useless unless it’s listening to some publishers and rendering the messages to the subscribers. So, let’s create our publisher.js file.

#publisher.jsvar mqtt = require('mqtt');
var client = mqtt.connect('mqtt://your_ip_address_of_pc');
client.on('connect', function () {setInterval(function() {
client.publish('myTopic', 'Hello mqtt');
console.log('Message Sent');
}, 5000);
});

Both the publisher and subscriber have to require mqtt package in order to avail all the functions to get working with it. Connecting the broker needs a domain name or corresponding IP address (i.e. iot.eclipse.org- which is a free broker). In this case, we are using our pc as broker, so we have to put the IP address of our pc. To know your ip address, type as follows in cmd (windows), get it and put it right where ”your ip_address_of_pc” is written above:

ipconfig

I suppose you already understand the latter part which demonstrates to publish a message on a particular topic. If you don’t, you can follow this link where I have explained the entire process in the most convenient way.

Here we go with our subscriber.js file.

#subscriber.jsvar mqtt = require('mqtt')
var client = mqtt.connect('mqtt://your_ip_address_of_pc')
client.on('connect', function () {
client.subscribe('myTopic')
})client.on('message', function (topic, message) {context = message.toString();
console.log(context)
})

There should be no ambiguity or confusion in the above code snippet. However, just because the data comes in the form of a buffer, to print it in the console as raw string, I used the toString() function.

You can obviously put all three code snippets together in a single file, but there’s something called clean code you know! So, I ran the project in three separate files, and got a chanting output like this:

Well, if you wish, you can now test your mqtt connection through other mqtt clients like android’s mqtt dashboard, chrome’s mqtt lens extension and so forth using your pc’s ip address as broker ip.


I am ending it here. Feel free to ask me any queries regarding this project. If there’s any mistakes or suggestions, please let me know.

Have fun with your self-made private broker! :)

Alif Abdullah

Written by

I'm looking for lives in Data

More From Medium

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade