node package manager
Easy sharing. Manage teams and permissions with one click. Create a free org »

rayson

rayson

Build Dependencies

Rayson is a JSON-RPC 2.0 mqtt client and mqtt server for node.js based on jayson.

Installation

$ npm install --save rayson

Server

jason-mqtt server played as a mqtt consumer, so it need a mqtt server start first. We can use mosca.

Exposes an array of functions which retrieves and returns data.

var rayson = require('../'/*'rayson'*/);
 
var moscaServer = new require('mosca').Server({port: 9999}); // start mosca server for test 
 
var server = rayson.server({
    localtime: function (cb) {
        console.log('localtime has been called');
        cb(null, new Date());
    }
}).mqtt('mqtt://localhost:9999', '$rpc/server1/localtime');
 
server.format('msgpack'); // default is `json` 
 

Client

Consumes the api exposed by the previous example.

var rayson = require('../'/*'rayson'*/);
 
var client = rayson.client.mqtt('mqtt://localhost:9999', '$rpc/:sid/localtime');
 
client.format('msgpack'); // default is `json` 
 
client.mqtt.on('error', function (err) {
    console.error(err);
});
 
client.mqtt.on('connect', function () {
    console.log('connected');
});
 
client.mqtt.on('reconnect', function () {
    console.log('reconnect');
});
 
client.mqtt.on('offline', function () {
    console.log('offline');
});
 
client.sid('server1').request('localtime', [], function(err, error, time) {
    console.log('localtime: ', time);
}).timeout(10);
 

License

MIT © taoyuan