This document provides an overview of Socket.IO, a JavaScript library for real-time web applications. It discusses what Socket.IO is, how it provides persistent connections and real-time functionality across browsers including older versions of Internet Explorer. It also summarizes how to install and use Socket.IO on both the client-side and server-side as well as how to send and receive events. Key features like broadcasting messages, acknowledgements, and configurations are also highlighted. Upcoming releases like optimizations, gzip support, and integration with Redis for scaling are mentioned as well.
2. who am I stalk me on
twitter
socket.io team member
winner node knockout 2011
early noder since 0.1.3 @3rdEden
all around performance / high
availability & scalability geek.
does not like coffeescript ;)
14. var http = require(‘http’);
var s = http.createServer(function(q,r){
r.writeHead(200); r.end(‘sup’);
});
s.listen(80);
var io = require(‘socket.io).listen(s);
listening on the server
15. var io = require(‘socket.io).listen(80);
listening on the server
16. io.sockets.on(‘connection’, function (socket) {
socket.on(‘custom event’, function (data) {
socket.emit(‘custom event received’);
});
socket.on(‘disconnect’, function (){
console.log(‘client disconnected’);
});
});
listening on the server
21. socket.send(‘plain text message’);
socket.json.send({ json:‘here’ });
// send method triggers the message
// event
socket.on(‘message’, function (data) {
// data is plain text, or our json
});
sending messages client & server
23. // broadcasts a message to all sockets
socket.broadcast.emit(‘event’);
message flag
24. // no need to internally buffer this msg
socket.volatile.emit(‘event’);
message flag
25. // broadcast, but only to people in this room
socket.broadcast.to(‘room’).emit(‘event’);
message flag
26. // broadcast to everyone
io.sockets.emit(‘event’);
io.sockets.send(‘hello nodejsconf.it’);
message flag
27. // on the client
socket.send(‘hello nodejsconf.it’, function(arg) {
console.log(‘message reached the server’);
console.log(‘arg:’, arg); // ‘hello’
});
// on the server
socket.on(‘message’, function (msg, fn) {
console.log(‘msg:’, msg);
fn(‘hello’); // confirm the acknowledgement
});
acknowledgements
OMG YOU SEE MY SUPER SECRET HIDDEN PRESENTER SLIDE NOTES *shame* ;D\n
so hi, i’m arnout kazemier i’m one of the socket.io team members. After becoming second solo in last years node knockout competition i finally managed to become first solo, overall and utility. I have been using node since the early days and i’m a all around performance / high availability geek.\n
\n
\n
Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript.\n
cross browser, and cross platform compatible\n
Desktop\nInternet Explorer 5.5+, Safari 3+, Google Chrome 4+ ,Firefox 3+, Opera 10.61+\n\nMobile\niPhone Safari, iPod Safari, iPad Safari, Android WebKit, WebOs WebKit\n\n
\n
\n
Big projects are using it, like Cloud 9 ide\n
\n
\n
for connecting with a socket.io server out of node, this is actually also the client side code. but i’m not going to dive to deep in this.\n
once you got socket.io installed all that is left to do is have it listen to a HTTP server.\n
if you don’t have one, you can also supply it with number and we will generate one for you.\n
Now that we have attached socket.io on a server we can start listening for events. \n
The /socket.io/socket.io.js file is served by socket.io automatically. We ship a compatible client with the server so you are sure that your client side code is compatible with your socket.io server. But you can always just host the file your self\n
\n
\n
Follows the node.js EventEmitter pattern\n
These are used for lower level messaging. No fancy pancy abstractions on top of it\n
Message flags are used to reduce messaging complexity but still give you great control over the process.\nThe .json flag is available both on the server and on the client\n
To broadcast, simply add a broadcast flag to emit and send method calls. Broadcasting means sending a message to everyone else except for the socket that starts it.\n
Sometimes certain messages can be dropped. Let's say you have an app that shows realtime tweets for the keyword bieber.\nIf a certain client is not ready to receive messages (because of network slowness or other issues, or because he's connected through long polling and is in the middle of a request-response cycle), if he doesn't receive ALL the tweets related to bieber your application won't suffer.\nIn that case, you might want to send those messages as volatile messages.\n\n
Sometimes you want to put certain sockets in the same room, so that it's easy to broadcast to all of them together.\nThink of this as built-in channels for sockets. Sockets join and leave rooms in each socket.\n\n
But sometimes you just want to broadcast to all connected clients, so instead of emitting to the socket object you call the emit function on io.sockets\n\n
Sometimes you just want to be sure that a message reached the server\nSometimes, you might want to get a callback when the client confirmed the message reception.\nTo do this, simply pass a function as the last parameter of .send or .emit. What's more, when you use .emit, the acknowledgement is done by you, which means you can also pass data along:\n\n\n
\n
\n
We start off with a handshake request, this allows the server to authenticate the connecting client, accepting or declining the connection. During this step we also store a snapshot of the headers and ip address.\n
Once the handshake is accepted we return a connection id, accepted transports and some configuration options such the close timeout.\n
after the handshake we start searching a available transport layer using feature detection\n
Requires Flash 10+ and a policy file server (we ship this) has a slow start up time but it does bidirectional communication. This transport is disabled by default on the server because of the slow upstart time.\n
A IE only transport, it makes use of the ActiveX HTMLFile to create a forever loading iframe. The HTML allows this to work without showing any loading indicators.\n
A IE only transport, it makes use of the ActiveX HTMLFile to create a forever loading iframe. The HTML allows this to work without showing any loading indicators.\n
If everything else fails we can just use JSONP polling\n