How to setup a Mosca Node.js MQTT broker service on Ubuntu 14.04

May 4, 2015

This article will cover installing Mosca Node.js MQTT standalone broker service along side Redis on Ubuntu 14.04. MQTT is a light weight publish-subscription messaging protocol for use on top of the TCP/IP.

MQTT is designed for connections with remote locations where network bandwidth is limited. The Pub/Sub messaging pattern requires a message broker which is what is covered in this article. The broker distributes messages to clients subscribers based on the topic of a message.

Step 0
Time Matters!

sudo apt-get update
 
sudo ntpdate pool.ntp.org
 
sudo apt-get install ntp

Step 1
Install Redis

sudo apt-get install build-essential
 
sudo apt-get install tcl8.5
 
wget http://download.redis.io/releases/redis-3.0.0.tar.gz
 
gunzip -c redis-3.0.0.tar.gz | tar -xvf -
 
cd redis-3.0.0/
 
sudo make
 
sudo make install

Step 2
Start the Redis Server

cd utils/
 
sudo ./install_server.sh

Step 3
Install Mosca, and some helpful node packages

sudo apt-get update
 
sudo apt-get install nodejs
 
sudo ln -s /usr/bin/nodejs /usr/bin/node
 
sudo apt-get install npm
 
sudo npm install debug
 
sudo npm install mosca bunyan -g
 
sudo npm install daemon

Step 4
Create your Mosca Node.js MQTT Broker Service mosca-app.js

console.log(process.pid);
require('daemon')();
var mosca = require('mosca')
 
var backjack = {
  type: 'redis',
  db: 12,
  port: 6379,
  return_buffers: true,
  host: "localhost"
};
 
var moscaSettings = {
  port: 1883,
  backend: backjack,
  persistence: {
    factory: mosca.persistence.Redis
  }
};
 
var server = new mosca.Server(moscaSettings);
server.on('ready', setup);
 
server.on('clientConnected', function(client) {
    console.log('client connected', client.id);     
});
 
server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
});
 
function setup() {
  console.log('Mosca server is up and running')
}
console.log(process.pid);

Step 5
Start the Mosca Node.js MQTT Broker Service

node mosca-app.js

Step 6
Install MQTT Client For Testing

sudo npm install mqtt -g

Step 7
Write a MQTT Publishing Client client-pub.js

var mqtt    = require('mqtt');
var client  = mqtt.connect('mqtt://localhost');
 
client.on('connect', function () {
  client.publish('presence', 'Hello!', {retain: false, qa: 1});
client.end();
});

Step 8
Write a MQTT Subscription Client client-sub.js

var mqtt    = require('mqtt');
var client  = mqtt.connect('mqtt://localhost');
 
client.on('connect', function () {
  client.subscribe('presence');
 
client.on('message', function (topic, message) {
  console.log(message.toString());
client.end();
  });
});

Step 9
Open a New Terminal and Execute your MQTT Subscription Client client-sub.js

node client-sub.js

Step 10
Open a New Terminal and Execute your MQTT Publish Client client-pub.js

node client-pub.js

Successful Output From client-sub.js terminal

Hello!

I will write a followup article on scaling Mosca soon for production.

Comments for “How to setup a Mosca Node.js MQTT broker service on Ubuntu 14.04”

  1. Peter Retief commented on June 9, 2015

    Worked well for me – on Debian squeeze
    Nice to find the steps in one place

  2. Daratha Galkissa commented on September 18, 2015

    Thanks for the easy steps. It helped me to learn Mosca….!!

    For Ubuntu readers,
    if you find this “sh: 1: node: not found” error in following step
    “sudo npm install mqtt -g”

    try this :
    sudo apt-get install nodejs-legacy

  3. lakshmikandan commented on November 6, 2015

    Getting following error when run the server.js file
    # node server.js

    module.js:340
    throw err;
    ^
    Error: Cannot find module ‘socket.io’
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object. (/usr/local/it/server.js:16:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

Leave a Reply