Skip to content
This repository has been archived by the owner on Feb 11, 2020. It is now read-only.

Mosca basic usage

John Kokkinidis edited this page May 8, 2014 · 25 revisions

##Get me started Mosca can be used into any Node.js app. As mentioned before you can use any broker that ascoltatore offers.

###Ok first things first.. Lets configure the server:

==============================

A) Get mosca.
var mosca = require('mosca')
B) Configure the pub/sub settings with any one of the following. (If you ask yourself, why do we need this, read Q1 on FAQ )

In our example we will be using mongoDB, but you can check how to use others like Redis, Mosquitto, RabbitMQ, QlobberFSQ or ZeroMQ on Mosca advanced usage wiki page.

var pubsubsettings = {
  //using ascoltatore
  type: 'mongo',		
  url: 'mongodb://localhost:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};
C) Then we just pass the pub/sub settings object (the one we created above) to our server (into our moscaSettings), through the 'backend' key.
var moscaSettings = {
  port: 1883,			//mosca (mqtt) port
  backend: pubsubsettings	//pubsubsettings is the object we created above 

};

var server = new mosca.Server(moscaSettings);	//here we start mosca
server.on('ready', setup);	//on init it fires up setup()

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

Sending data from mosca to clients:

The publish() function allows to programatically publish a value to MQTT clients with full support of all distinctive MQTT features: offline, quality of server, and retained messages.

var message = {
  topic: '/hello/world',
  payload: 'abcde', // or a Buffer
  qos: 0, // 0, 1, or 2
  retain: false // or true
};

server.publish(message, function() {
  console.log('done!');
});

Receiving data from clients:

The on() function allows to programatically listen for messages received from the client side.

// fired when a message is published
server.on('published', function(packet, client) {
console.log('Published', packet);
console.log('Client', client);
});
// fired when a client connects
server.on('clientConnected', function(client) {
console.log('Client Connected:', client.id);
});

// fired when a client disconnects
server.on('clientDisconnected', function(client) {
console.log('Client Disconnected:', client.id);
});

Taratataaaa. And now, its time for a simple fully working example!

###Lets put it all together now:

var mosca = require('mosca')

var ascoltatore = {
  //using ascoltatore
  type: 'mongo',		
  url: 'mongodb://localhost:27017/mqtt',
  pubsubCollection: 'ascoltatori',
  mongo: {}
};

var moscaSettings = {
  port: 1883,
  backend: ascoltatore
};


server.on('clientConnected', function(client) {
	console.log('client connected', client.id);		
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
});

//in case of an error
process.on("uncaughtException", function(error) {
  return console.log(error.stack);
});


var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

###In this example we will be using redis.

var mosca = require('mosca')

var ascoltatore = {
  type: 'redis',
  redis: require('redis'),
  db: 12,
  port: 6379,
  return_buffers: true, // to handle binary payloads
  host: "localhost"
};

var moscaSettings = {
  port: 1883,
  backend: ascoltatore
};


server.on('clientConnected', function(client) {
	console.log('client connected', client.id);		
});

// fired when a message is received
server.on('published', function(packet, client) {
  console.log('Published', packet.payload);
});

//in case of an error
process.on("uncaughtException", function(error) {
  return console.log(error.stack);
});


var server = new mosca.Server(moscaSettings);
server.on('ready', setup);

// fired when the mqtt server is ready
function setup() {
  console.log('Mosca server is up and running')
}

For more advanced features make sure you check the Mosca advanced usage wiki page.