Show Posts
← Back to homepage

Over at LearnBoost, I just released Socket.IO, a normalized Socket API that hides the complexity of the realtime transports.

With code as simple as this:

socket = new io.Socket('localhost');
socket.connect();
socket.send('some data');
socket.addEvent('message', function(data){
    alert('got some data' + data);
});

you’ll be leveraging:

  • WebSocket
  • Adobe Flash Socket
  • ActiveX HTMLFile (IE)
  • Server-Sent Events (Opera)
  • XHR with multipart encoding
  • XHR with long-polling

And on the server side, things are equally simple. This is a chat application written in Socket.IO-node, the Node.JS backend implementation:

var buffer = [], json = JSON.stringify;

io.listen(server, {

	onClientConnect: function(client){
		client.send(json({ buffer: buffer }));
		client.broadcast(json({ announcement: client.sessionId + ' connected' }));
	},

	onClientDisconnect: function(client){
		client.broadcast(json({ announcement: client.sessionId + ' disconnected' }));
	},

	onClientMessage: function(message, client){
		var msg = { message: [client.sessionId, message] };
		buffer.push(msg);
		if (buffer.length > 15) buffer.shift();
		client.broadcast(json(msg));
	}

});

As usual, head to GitHub to experiment with the code.

13 Comments

davetiye said

thank you wery much

Reinhardt said

Why do you use RosePad instead of LearnBoost? It seems RosePad does not exist at Github. It also causes trouble when you update submodules. Any suggestions would be great!

iwanido said

Festivus for the rest of us! hahahaha

Kostya said

thank you,

all works, except event ‘onClientMessage’

please, tell, in what there can be a problem!

…sorry for my english

Saas said

thanks for the help.

Dean said

Love the website :) Great ideas and resources! :P Takes the pain out of being a developer!

Davetiye said

This the exact response what i was looking for. Thanks very much.

I agree with Reinhardt..Pls tell us why you used Rosepad? Thank you in advance

Thank you for the detailed information..Regards

Thanks for sharing, they have such cute designs.gave a very creative idea for
me.

nikah şekeri

Your thoughts?

About Guillermo Rauch:

CTO and co-founder of LearnBoost, developer, open source enthusiast, blogger.