WebSocket over Node.js / Demo
This experiment is using WebSocket over Node.js for signaling.
Install via npm
npm install websocket-over-nodejs
and run the signaler.js
nodejs file:
node node_modules/websocket-over-nodejs/signaler.js
Otherwise, follow these steps:
- Download and extract ZIP file of this repository then copy
folder-location
. - Open Node.js command prompt.
- Type command
cd folder-location
wherefolder-location
can beC:\websocket-over-nodejs
. - Type
node signaler
to run the node.js server.
OK, now you can listen websocket URL like this:
var websocket = new WebSocket('ws://localhost:8888/');
If you want to deploy your application
First of all; change subdomain in the package.json
file:
{
"name": "just-a-name",
"subdomain": "must-be-unique",
"scripts": {
"start": "signaler.js"
},
"version": "0.0.0",
"engines": {
"node": "0.10.x"
},
"dependencies": {
"websocket": "1.0.x"
}
}
- Create an account at
nodejitsu
- Use same Node.js command prompt window
- Type
jitsu deploy
and you're done!
Remember: jitsu deploy
command will deploy the entire directory containing all all files including node_modules
(i.e. dependencies).
Now, you can listen your nodejitsu server like this:
// Remember, must include port "80"!
var websocket = new WebSocket('ws://subdomain.jit.su:80');
How to use?
In ui.js
files you can find openSocket
method; or in all libraries; you can find openSignalingChannel
method.
// ws://wsnodejs.jit.su:80
var SIGNALING_SERVER = 'ws://' + document.domain + ':1338/';
connection.openSignalingChannel = function(config) {
config.channel = config.channel || this.channel || 'default-channel';
var websocket = new WebSocket(SIGNALING_SERVER);
websocket.channel = config.channel;
websocket.onopen = function() {
websocket.push(JSON.stringify({
open: true,
channel: config.channel
}));
if (config.callback)
config.callback(websocket);
};
websocket.onmessage = function(event) {
config.onmessage(JSON.parse(event.data));
};
websocket.push = websocket.send;
websocket.send = function(data) {
websocket.push(JSON.stringify({
data: data,
channel: config.channel
}));
};
}
Signaling Concepts
Interested to understand WebRTC Signaling Concepts? Read this document.
License
MIT. Copyright (c) 2014 webrtclabs.