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.
if ( comments_open() ) { ?>
13 Comments
Pingback: Какво ново в третата седмица на Март | NeXt
thank you wery much
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!
Pingback: JSConf 2010 Slides | RefreshTheNet
Festivus for the rest of us! hahahaha
thank you,
all works, except event ‘onClientMessage’
please, tell, in what there can be a problem!
…sorry for my english
thanks for the help.
Love the website
Great ideas and resources!
Takes the pain out of being a developer!
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