-
-
Notifications
You must be signed in to change notification settings - Fork 508
Mosca basic usage
Mosca can be used into any Node.js app. As mentioned above you can use any broker that ascoltatore offers.
###Ok first things first.. Lets configure the server:
Get mosca.
var mosca = require('mosca')
Then configure the pub/sub settings with any one of the following. (If you ask yourself, why do we need this, read Q1 on FAQ )
Example of pub/sub settings using:
- MongoDB
var pubsubsettings = {
//using ascoltatore
type: 'mongo',
url: 'mongodb://localhost:27017/mqtt',
pubsubCollection: 'ascoltatori',
mongo: {}
};
- Redis
var pubsubsettings = {
type: 'redis',
redis: require('redis'),
db: 12,
port: 6379,
return_buffers: true, // to handle binary payloads
host: "localhost"
};
- Mosquitto (! this uses an existing mqtt broker called mosquitto and not mosca as our mqtt server)
var pubsubsettings = {
type: 'mqtt',
json: false,
mqtt: require('mqtt'),
host: '127.0.0.1',
port: 1883
};
- AMQP (RabbitMQ) (! this uses an AQMP server, and not mosca as our mqtt server)
var pubsubsettings = {
type: 'amqp',
json: false,
amqp: require('amqp'),
exchange: 'ascolatore5672'
};
- QlobberFSQ (! this uses an AQMP server, and not mosca as our mqtt server) You can use any of the QlobberFSQ constructor options, for example:
var pubsubsettings = {
type: 'zmq',
json: false,
zmq: require("zmq"),
port: "tcp://127.0.0.1:33333",
controlPort: "tcp://127.0.0.1:33334",
delay: 10
};
- Nothing or just use no settings at all to store everything in memory and not in a pub/sub broker
Then we just pass the settings object (the one we created above) on our server, through the 'backend' key. (! of course if you choose nothing from the above pub/sub brokers, theres no need to configure the 'backend' var, so you only configure the port.)
var settings = {
port: 1883, //mosca (mqtt) port
backend: pubsubsettings //pubsubsettings is the object we created above
};
var server = new mosca.Server(settings); //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')
}
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!');
});
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);
});
###A few known events to listen for:
- clientConnected : when a client is connected; the client is passed as a parameter.
- clientDisconnecting : when a client is being disconnected; the client is passed as a parameter.
- clientDisconnected : when a client is disconnected; the client is passed as a parameter.
- published : when a new message is published; the packet and the client are passed as parameters.
- subscribed : when a client is subscribed to a topic; the topic and the client are passed as parameters.
- unsubscribed : when a client is unsubscribed to a topic; the topic and the client are passed as parameters.
In this example we will be using redis.
var mosca = require('mosca')
// var ascoltatore = {
// type: 'mongo',
// url: 'mongodb://localhost:27017/mqtt',
// pubsubCollection: 'ascoltatori',
// mongo: {}
// };
var ascoltatore = {
type: 'redis',
redis: require('redis'),
db: 12,
port: 6379,
return_buffers: true, // to handle binary payloads
host: "localhost"
};
var settings = {
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(settings);
server.on('ready', setup);
// fired when the mqtt server is ready
function setup() {
console.log('Mosca server is up and running')
}