Skip to content

Instantly share code, notes, and snippets.

@eiriklv
Forked from benfoxall/hacky-talk-server.js
Created March 19, 2014 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eiriklv/9650225 to your computer and use it in GitHub Desktop.
Save eiriklv/9650225 to your computer and use it in GitHub Desktop.
// *******************
// file server on 3000
(function(){
var express = require('express'),
app = express(),
server = require('http').createServer(app);
// serve static files
app.use(express.static(__dirname + '/public'));
server.listen(3000);
}());
// *******************
// socket.io on 3001
(function(){
var app = require('http').createServer(handler),
io = require('socket.io').listen(app),
fs = require('fs');
app.listen(3001);
// quiet IO!
io.set('log level', 2);
function handler (req, res) {
res.writeHead(200);
res.end('hello');
}
io.sockets
.on('connection', function (socket) {
socket.on('message', function (data) {
console.log("data>", data);
socket.broadcast.send(data);
});
});
}());
// *******************
// binaryjs on 3002
(function(){
//Start Binary.js server
var BinaryServer = require('binaryjs').BinaryServer;
var bs = BinaryServer({port:3002});
// Wait for new user connections
bs.on('connection', function(client){
client.on('data', function(d){
console.log("DATA>", d)
});
// Incoming stream from browsers
client.on('stream', function(stream, meta){
for(var id in bs.clients){
if(bs.clients.hasOwnProperty(id)){
var otherClient = bs.clients[id];
if(otherClient !== client){
console.log('sending: ' + client.id + '---->' + otherClient.id);
otherClient.send("SUP?");
var send = otherClient.createStream(meta);
stream.pipe(send);
}
}
}
stream.on('data', function(data){
console.log("binaryjs>", data.length);
});
});
});
}())
// *******************
// webrtc on 3003
var webRTC = require('webrtc.io').listen(3003);

I'm planning to write a proper blog post with descriptions, examples & demos, but here are a couple of resources to get up and hacking with websockets and webrtc.

Websockets

Probably the easiest/fastest way of getting this set up is using socket.io, it falls back to other methods for older browsers. You could use something like ws for a purer/'proper' websockets.

To send binary files (like photos in <input> fields in my example) - you have to use binaryjs to chunk up the file for sending.

WebRTC

apprtc.appspot.com is definitely worth a look through - webRTC.io can be quite a handy abstraction layer.


In my talk - I hacked together a node server that broadcast any messages posted to the other clients (code on this gist). They were all on different ports to keep things simple.

The client code is a bit confusing at the moment, though I'll hopefully bring it together soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment