It is a node.js and socket.io based reliable signaling implementation. Remember, reliable doesn't mean "scalable"; reliable simply means that it auto reconnects in any kind of failure or internet disconnect. It is having following features:
- Auto reconnects if node.js gets down out of any reason.
- Auto reconnects if internet connection disconnects.
- It provides custom-signaling for your RTCMultiConnection and DataChannel applications!
npm install reliable-signaler
- You can store a room-id on server using
createNewRoomOnServer
method. - You can get that room-id using
getRoomFromServer
method.
- In your Node.js server, invoke
require('reliable-signaler')
and pass HTTP-Server object. - In your HTML file, link this script:
/reliable-signaler/signaler.js
- In your
<script>
tag, invokeinitReliableSignaler
constructor. - Invoke
createNewRoomOnServer
method for room-moderator. - Invoke
getRoomFromServer
method from room-participants (multiple participants).
# install rtcmulticonnection-client
npm install rtcmulticonnection-client
node ./node_modules/rtcmulticonnection-client/server.js
# or intall datachannel-client
npm install datachannel-client
node ./node_modules/datachannel-client/server.js
# or intall videoconferencing-client
npm install videoconferencing-client
node ./node_modules/videoconferencing-client/server.js
Now open localhost port:8080
.
To use it in your node.js code: (required)
var httpServer = require('http').createServer(callback);
require('reliable-signaler')(httpServer || expressServer || portNumber, {
// for custom socket handlers
socketCallback: function(socket) {
socket.on('custom-handler', function(message) {
socket.broadcast.emit('custom-handler', message);
});
}
});
Constructor of the module reliable-signaler
takes an config
object where you can pass socketCallback
and other configurations:
var config = {
socketCallback: function(socket) {}
};
require('reliable-signaler')(httpServer, config);
*. socketCallback
: If you want to attach custom handlers over socket object.
To use it in the browser: (required)
<script src="/reliable-signaler/signaler.js"></script>
And your client-side javascript code:
var connection = new RTCMultiConnection();
// invoke "initReliableSignaler" and pass "connection" or "channel" object
var signaler = initReliableSignaler(connection, 'http://domain:port/');
Call createNewRoomOnServer
method as soon as you'll call open
method. You can even call createNewRoomOnServer
earlier than open
however it isn't suggested:
For RTCMultiConnection:
// for data-only sessions
connection.open();
signaler.createNewRoomOnServer(connection.sessionid);
// or (not recommended)
signaler.createNewRoomOnServer(connection.sessionid, function() {
connection.open();
});
// or --- recommended.
connection.open({
onMediaCaptured: function() {
signaler.createNewRoomOnServer(connection.sessionid);
}
});
For DataChannel:
channel.open('room-id');
signaler.createNewRoomOnServer('room-id', successCallback);
For participants, call getRoomFromServer
method:
// RTCMultiConnection
signaler.getRoomFromServer('sessioin-id', function(roomid) {
// invoke "join" in callback
connection.join({
sessionid: roomid,
userid: roomid,
extra: {},
session: connection.session
});
// or simply
connection.join(roomid);
// or
connection.connect(roomid);
});
// DataChannel
signaler.getRoomFromServer('sessioin-id', function(roomid) {
channel.join({
id: roomid,
owner: roomid
});
// or
channel.connect(roomid);
});
<script src="/reliable-signaler/signaler.js"></script>
<script>
var connection = new RTCMultiConnection();
var signaler = initReliableSignaler(connection, '/');
btnOpenRoom.onclick = function() {
connection.channel = connection.sessionid = connection.userid = sessionid;
connection.open({
onMediaCaptured: function() {
signaler.createNewRoomOnServer(connection.sessionid);
}
});
};
btnJoinRoom.onclick = function() {
signaler.getRoomFromServer(roomid, function(roomid){
connection.channel = connection.sessionid = roomid;
connection.join({
sessionid: roomid,
userid: roomid,
extra: {},
session: connection.session
});
});
};
</script>
<script src="/reliable-signaler/signaler.js"></script>
<script>
var channel = new DataChannel();
var signaler = initReliableSignaler(channel, '/');
btnOpenRoom.onclick = function() {
signaler.createNewRoomOnServer(roomid, function() {
channel.channel = channel.userid = roomid;
channel.open(roomid);
});
};
btnJoinRoom.onclick = function() {
signaler.getRoomFromServer(roomid, function(roomid){
channel.channel = roomid;
channel.join({
id: roomid,
owner: roomid
});
});
};
</script>
Constructor takes either RTCMultiConnection
instance or a config
object:
# 1st option: Pass RTCMultiConnection object
var signaler = initReliableSignaler(connection);
# 2nd option: Pass "config" object
var signaler = initReliableSignaler(connection, '/');
initReliableSignaler
global-function exposes/returns 3-objects:
socket
createNewRoomOnServer
getRoomFromServer
// "socket" object
signaler.socket.emit('message', 'hello');
// "createNewRoomOnServer" method
signaler.createNewRoomOnServer(connection.sessionid, successCallback);
// "getRoomFromServer" object
signaler.getRoomFromServer('sessioin-id', callback);
This method simply takes sessioin-id
and stores in node.js server. You can even pass successCallback
.
signaler.createNewRoomOnServer(roomid, successCallback);
This method looks for active sessioin-id
in node.js server. Node.js server will fire callback only when session is found.
If session is absent, then node.js server will wait until someone opens that session; and node.js will fire getRoomFromServer-callback
as soon a session is opened.
signaler.getRoomFromServer(roomid, successCallback);
Reliable-Signaler is released under MIT licence . Copyright (c) Muaz Khan.