Signalling for rtc-io components using primus
This is an rtc.io signalling server (counterpart to rtc-signaller) that makes use of the excellent realtime abstraction library, primus. It has been designed and built primarily as a reference implementation for a signalling server and is not designed to be deployed at scale.
If you would like to our test signalling server (no uptime guaranteed) then you can use rtc-quickconnect and take it for a spin:
var quickconnect = require'rtc-quickconnect';quickconnect'//switchboard.rtc.io/' room: 'switchboard-test'createDataChannel'test'once'channel:opened:test'console.log'received data: ' evtdata;;dcsend'hello';;
Other examples are available in the guidebook
If you wish to use
rtc-switchboard on it's own to test signalling,
then you can simply clone this repository, install dependencies and start
git clonecd rtc-switchboardnpm installnode server.js
If you wish to run the server on a specific port, then set the
environment variable prior to execution:
NODE_PORT=8997 node server.js
To create an application using primus signalling, see the following examples:
var server = require'http'createServer;var switchboard = require'./'server servelib: true ;var port = parseIntprocessenvNODE_PORT || processenvPORT || processargv2 10 || 3000;var replify = require'replify';serveron'request'if requrl === '/'reswriteHead302'Location': '';resend'switchboard available from: ';;// start the serverserverlistenportif errreturn console.log'Encountered error starting server: ' err;console.log'server running at :' + port + '/';;// add the replreplifyname: 'switchboard'app: switchboardcontexts:server: server;switchboardon'room:create'console.log'room ' + room + ' created, now have ' + switchboardroomslength + ' active rooms';;switchboardon'room:destroy'console.log'room ' + room + ' destroyed, ' + switchboardroomslength + ' active rooms remain';if typeof gc == 'function'console.log'gc';gc;;
var express = require'express';var app = express;var server = require'http'Serverapp;var port = processenvPORT || 3000;// create the switchboardvar switchboard = require'rtc-switchboard'server;// we need to expose the primus libraryappget'/rtc.io/primus.js' switchboardlibrary;serverlistenportif errreturn;console.log'server listening on port: ' + port;;
rtc-switchboard makes use of the slick WebSockets abstraction library
Primus. To work with the server, you
will need to include the
primus.js library in your application prior to
attempting a websocket connection.
If you are working with a local standalone server, the following script tag will likely do the job:
NOTE: A specific call to include primus is not required if you are working with particular rtc.io library (such as rtc-glue), as they will ensure the primus library has been included prior to running their internal code.
If you are interested in deploying an instance of
docker then the following is a great place to
When you initialize the switchboard, you are able to provide custom handlers
for specific commands that you want handled by the switchboard. Imagine
for instance, that we want our switchboard to do something clever when a
client sends an
We would create our server to include the custom
img command handler:
var server = require'http'createServer;var Primus = require'primus';// create the signaller, providing our own primus instance (using engine.io)var switchboard = require'rtc-switchboard'serverservelib: truehandlers:img: require'./handlers/img';// start the serverserverlisten3000;
And then we would write a small module for the handler:
console.log'received an img command with payload: ' payload;;
Every message that flows through the switchboard (whether handled or not)
can be logged through tapping into the
data event. The example below
demonstrates how this can be done with a node logging module like
var express = require'express';var app = express;var server = require'http'Serverapp;var port = processenvPORT || 3000;var bunyan = require'bunyan';var log = bunyancreateLogger name: 'rtc-switchboard' ;// create the switchboardvar switchboard = require'rtc-switchboard'server;// we need to expose the primus libraryappget'/rtc.io/primus.js' switchboardlibrary;serverlistenportif errreturn;console.log'server running at: :' + port + '/';;switchboardon'data'log.info peer: peerId 'received: ' + data;;
As can be seen in the example above, the handlers of the
data event can
expect to receive three arguments to the handler function, as per the code
data is the raw data of that has been sent from the client, the
peerId is the id of the peer sending the data (this will be
it is a message received prior to an
/announce command). Finally we have
the raw primus
spark that can be examined for additional information.
Create the switchboard which uses primus under the hood. By default calling
this function will create a new
Primus instance and use the
pure websockets adapter.
The socket server is configured to handle some specific rtc.io signaller
messages. The handlers are stored in the
handlers/ folder of the
repository and have details outlined below.
/announce messages and associate the peer id assigned by
the client-side signaller with the socket on the server side.
This will allow routing of messages to the correct receipient when
/to messages are received.
Copyright 2014 National ICT Australia Limited (NICTA)
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.