Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created November 27, 2012 01:31
Show Gist options
  • Save fideloper/4151820 to your computer and use it in GitHub Desktop.
Save fideloper/4151820 to your computer and use it in GitHub Desktop.
Engine.io and server Client.io
// Install with 'npm install engine.io-client'
// Run in node.js as a client! (Not browser-only!)
/* Socket client */
var eio = require('engine.io-client');
var socket = new eio.Socket({ host: 'localhost', port: 8080 });
socket.onopen = function () {
socket.onmessage = function (data) {
console.log( JSON.parse(data.data) );
};
socket.onclose = function () {
};
};
//Install with 'npm install engine.io'
/* Socket server */
var engine = require('engine.io')
, server = engine.listen(8080)
// Send message to everyone but (optionally) the sending client
server.broadcast = function(mssg, id) {
for( var key in server.clients ) {
if(typeof id !== 'undefined') {
// Don't broadcast to sending client
if( key == id ) {
continue;
}
}
server.clients[key].send(mssg);
}
}
server.on('connection', function (socket) {
// Broadcast a to other cients
server.broadcast( JSON.stringify({resp:'New Client!'}), socket.id);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment