LearnBoost

Socket.IO

Download

Socket.IO Client


Socket.IO Node.JS server

Introduction

Socket.IO aims to provide a really simple API to leverage Sockets in the client side. It hides the complexity of the different transport mechanisms used to achieve realtime communication between a browser and a server today.

How to use

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

Features

Documentation

io.Socket

new io.Socket(host, [options]);

Options:

The resource is what allows the socket.io server to identify incoming connections by socket.io clients. In other words, any HTTP server can implement socket.io and still serve other normal, non-realtime HTTP requests.

Properties:

Methods:

Events:


Socket.IO Node.JS server

How to use

var http = require('http'), 
          io = require('./socket.io/socket.io.js'),

server = http.createServer(function(req, res){
    // your normal server code
    res.writeHeader(200, {'Content-Type': 'text/html'});
    res.writeBody('<h1>Hello world</h1>');
    res.finish();
});

// socket.io, I choose you
io.listen(server);

Documentation

Listener

io.listen(<http.Server>, [options])

Returns: a Listener instance

Public Properties:

Methods:

Options:

The resource is what allows the socket.io server to identify incoming connections by socket.io clients. Make sure they're in sync.

Events:

Important note: this in the event listener refers to the Listener instance.

Client

Client(listener, req, res)

Public Properties:

Methods:

Protocol

One of the design goals is that you should be able to implement whatever protocol you desire without Socket.IO getting in the way. Socket.IO has a minimal, unobtrusive protocol layer. It consists of two parts:

Despite this extra layer, your messages are delivered unaltered to the different event listeners. You can JSON.stringify() objects, send XML, or maybe plain text.