Using multiple nodes

Sticky load balancing If you plan to distribute the load of connections among different processes or machines, you have to make sure that requests associated with a particular session id connect to the process that originated them. This is due to certain transports like XHR Polling or JSONP Polling relying on firing several requests during the lifetime of the “socket”. To illustrate why this is needed, consider the example of emitting an event to all connected clients: io.emit('hi', 'all socket

Socket#to

Socket#to(room:String):Socket

Socket#rooms

Socket#rooms:Array A list of strings identifying the rooms this socket is in.

Socket#request

Socket#request:Request A getter proxy that returns the reference to the request that originated the underlying engine.io Client. Useful for accessing request headers such as Cookie or User-Agent.

Socket#leave

Socket#leave(name:String[, fn:Function]):Socket Removes the socket from room, and fires optionally a callback fn with err signature (if any). Rooms are left automatically upon disconnection. The mechanics of leaving rooms are handled by the Adapter that has been configured (see Server#adapter above), defaulting to socket.io-adapter.

Socket#join

Socket#join(name:String[, fn:Function]):Socket Adds the socket to the room, and fires optionally a callback fn with err signature (if any). The socket is automatically a member of a room identified with its session id (see Socket#id). The mechanics of joining rooms are handled by the Adapter that has been configured (see Server#adapter above), defaulting to socket.io-adapter.

Socket#in

Socket#in(room:String):Socket Sets a modifier for a subsequent event emission that the event will only be broadcasted to sockets that have joined the given room. To emit to multiple rooms, you can call to several times. var io = require('socket.io')(); io.on('connection', function(socket){ socket.to('others').emit('an event', { some: 'data' }); });

Socket#id

Socket#id:String A unique identifier for the socket session, that comes from the underlying Client.

Socket#emit

Socket#emit(name:String[, …]):Socket Emits an event to the socket identified by the string name. Any other parameters can be included. All datastructures are supported, including Buffer. JavaScript functions can’t be serialized/deserialized. var io = require('socket.io')(); io.on('connection', function(socket){ socket.emit('an event', { some: 'data' }); });

Socket#conn

Socket#conn:Socket A reference to the underyling Client transport connection (engine.io Socket object).