-
Notifications
You must be signed in to change notification settings - Fork 0
Home
This module is a transportation with WebSocket protocol, which is used by the RPC interface of freebird framework. It provides methods to create RPC client and RPC server for real-time remote communications. The RPC server should be registered to freebird framework to be able to transmit freebird messages to RPC client (e.g., the web browser). And the RPC client is provided for webapp to be able to communicate with freebird.
The freebird framework has net, dev, and gad subsystems responsible for network, device, and gadget management, respectively. In brief, a network is formed with many devices, and each device may have some gadgets on it. A gadget is the real application in a manchine network.
Let's take a wifi weather station in a machine network for example. The weather station is made up of temperature, humidity, and pm2.5 sensors, where each sensor is a gadget living on the WiFi-connected device. A device, such as Arduino(wifi-connected), ESP8266, MT7688, RaspberryPi or Beaglebone, is the carrier of applications(gadgets). Now we know, this weather station has 3 gadgets on it, but only has a single device in it.
Here is another example, we have a bluetooth low-energy (BLE) light switch in the network. This is a simple one-device-with-one-gadget machine, here we can say "There is only one gadget - a light switch - implemented on a TI CC2540 BLE SoC device."
The concept of net, dev, and gad subsystems in freebird framework well separates the machine management and application management from each other. This brings developers a more clear, convenient and flexible way in building up a IoT machine network.
$ npm install freebird-rpc --save
freebird-rpc exports its functionalities as a object, which contains two methods, createServer() and createClient().
- Create a RPC server
var fbRpc = require('freebird-rpc'),
http = require('http');
var httpServer = http.createServer();
httpServer.listen(3000);
var rpcServer = fbRpc.createServer(httpServer);
- Create a RPC client
var fbRpc = require('freebird-rpc');
var rpcClient = fbRpc.createClient('ws://192.168.1.108:3000');
Create the RPC server with a WebSocket server.
Arguments:
-
httpServer
(Object): A Node.js HTTP server.
Returns:
- (Object):
rpcServer
Example:
var http = require('http'),
fbRpc = require('freebird-rpc');
var httpServer,
rpcServer;
httpServer = http.createServer().listen(3000);
rpcServer = fbRpc.createServer(httpServer);
Create the RPC client with a WebSocket client.
Arguments:
-
addr
(String): host address -
options
(Object): An object to set up the websocket client. Please refer to ws.md to see more detail about options.
Returns:
- (Objects):
rpcClient
Example:
var rpcClient = fbRpc.createClient('ws://192.168.1.108:3000');
fbRpc.createServer()
returns an instance of this class. The instance, which is denoted as rpcServer
in this document, and cotains a WebSocket server to do real-time bidirectional with WebSocket client.
Send the response message to Websocket client with the results of the client asking for.
Arguments:
-
msg
(Object):msg
must containsclientId
anddata
properties.clientId
is used to identify the client you want to send message to, anddata
is the message to transmit out which must follow Response Data Object and should be converts to a JSON string. -
callback
(Function):function (err, bytes) { ... }
. Get called after message send off. The argumentbytes
tells how many bytes were sent.
Returns:
- (None)
Example:
var sendData = {
__intf: 'RSP',
subsys: 'net',
seq: 5,
id: null,
cmd: 'getAllDevIds',
status: 0,
data: { ids: [ 1, 2, 3, 8, 12 ] }
};
sendData = JSON.stringify(sendData);
rpcServer.send({ clientId: 'client01', data: sendData }, function (err, bytes) {
if (err)
console.log(err);
else
console.log(bytes + ' bytes were sent');
});
Broadcast the indication message to all WebSocket clients.
Arguments:
-
msg
(Object):msg
must containsdata
property.data
is the message to transmit out which must follow Indication Data Object and should be converts to a JSON string. -
callback
(Function):function (err, bytes) { ... }
. Get called after message send off. The argumentbytes
tells how many bytes were sent.
Returns:
- (None)
Example:
var broadcastData = {
__intf: 'IND',
subsys: 'net',
type: 'started',
id: null,
data: { netcore: 'freebird-netcore-ble' }
};
broadcastData = JSON.stringify(broadcastData);
rpcServer.broadcast({ data: broadcastData }, function (err, bytes) {
if (err)
console.log(err);
else
console.log(bytes + ' bytes were sent');
});
The user can listen to the 'message'
event to receive the request message from WebSocket client.
Arguments of the listener
-
msg
(Object):msg
containsclientId
anddata
properties.clientId
is to help the user determine which client sends the message, anddata
is the received message which will follow Request Data Object and be converted to a JSON string.
Example:
rpcServer.on('message', function (msg) {
console.log(msg.data.toString());
});
fbRpc.createClient()
returns an instance of this class. The instance, which is denoted as rpcClient
in this document, and cotains a WebSocket client to do real-time bidirectional with WebSocket server.
Send the request message to Websocket server to request something or to ask the server to perform an operation.
Arguments:
-
subSys
(String): Only 3 types accepted. They are 'net', 'dev', and 'gad' to denote which subsystem is this message going to. -
cmd
(String): Command Identifier corresponding to the API name. It can be found in the Command Name field of the Request Data Model. -
args
(Object): A value-object that contains command arguments. Please see section Request Data Model to learn more about the args data object. -
callback
(Function):function (err, result) {}
. Get called when server respond to client with the results of the client asking for.-
'err'
(Error): Error object. -
'result'
(Object): result is an object of{ status, data }
.status
is corresponding to RSP Status Code.data
is a response data object, you can refer to Response Data Model to see more detail.
-
Returns:
- (None)
Example:
rpcClient.send('net', 'getAllDevIds', { ncName: 'freebird-netcore-ble' }, function (err, result) {
if (err) {
console.log(err);
} else {
console.log(result);
// result equal to
// {
// status: 0,
// data: {
// ids: [1, 5, 8, 15, 27]
// }
// }
}
});
Emitted when the connection is open and ready to communicate.
Arguments of the listener
- (None)
Example:
rpcClient.on('open', function () {
// You can communicate to RPC server
});
Emitted when the connection is closed.
Arguments of the listener
-
code
(Number):code
is a numeric value indicating the status code explaining why the connection has been closed. -
reason
(String):reason
is a human-readable string explaining why the connection has been closed.
Example:
rpcClient.on('close', function (code, reason) {
console.log('Connection is closed because the reason: ' + reason);
});
Emitted when receiving an indication from websocket server side.
Arguments of the listener
-
msg
(Object): It is a message object with properties'subsys'
,'type'
,'id'
and'data'
-
subsys
(String): They are 'net', 'dev', and 'gad' to denote which subsystem is this indication coming from. -
type
(String): Indication types. Please see section Indication types for details. -
id
(Number): Id of the sender. id is meaningless ifsubsys === 'net'
. id is device id ifsubsys === 'dev'
. id is gadget id ifsubsys === 'gad'
-
data
(Object): Data along with the indication. Please see section Indication Data Model to learn more about the indication data format.
-
Example:
rpcClient.on('ind', function (msg) {
console.log(msg);
});
Overview
Usage
APIs & Events
- createClient()
- createServer()
- Client Class
- Server Class
- send()
- broadcast()
- 'message' event
Messaging Interface
REQ/RSP Message Model
IND Message Model
Appendix
- Data Objects
- Gadget classId
- Status Code