Mosca basic usage

Simba Zhang edited this page Apr 19, 2017 · 24 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 our 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 service, 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,
  persistence: {
    factory: mosca.persistence.Mongo,
    url: 'mongodb://localhost:27017/mqtt'
  }
};

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

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);
});

// 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,
  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);		
});

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

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

Note: If you are using a non-standard redis port or redis is running on a different server, you will also need to set host and port in the persistence section. eg:

persistence: {
  factory: mosca.persistence.Redis,
  host: 'your redis host',
  port: 'your redis port'
}

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