In this appendix we will take a look at the APIs provided by socket.io. The intention is to have a cursory glance through all the APIs so we know if there is a function that can help us while we are working. Socket.io is under active development and the APIs themselves are subject to change. Although the documented methods may not change, there are always new functions and features being added to socket.io. So always check with the socket.io website and wiki for the availability of a function that does what you want.
As we already know by now, socket.io provides libraries to be used both in the server and the client. Let's first see the APIs provided for the server.
The socket.io
module is instantiated, just like any other node module, by using require
to import the module:
var io = require('socket.io'),
The socket.io server component is started by using the listen
method, which attaches the socket.io to the node HTTP server:
var sio = io.listen(<server>)
Here, server
is the instance of the node HTTP server.
The event handlers are attached to socket using the on
method. The on
method takes the event name and the callback/handler function as parameters:
sio.on(<event>, function(eventData){ //DO SOMETHING });
Here, event
is the name of the event and eventData
represents the event-specific data passed to the handler when it is invoked.
We use the emit
method to trigger an event. This event will be handled on the client:
socket.emit(<event>, <event_data>, ack_callback);
Here, event
is the name of the event to trigger, event_data
is the event data as a JSON object, and ack_callback
is the optional callback function that is invoked on the successful receipt of the event on the client.
The send
method is used to send a message to the client:
socket.send(<message>, ack_callback);
Where message
is the message that will be sent to the client and ack_callback
is the optional callback function that is invoked on the successful receipt of the message on the client.
A JSON message can be sent by using the json
flag before the send
method:
socket.json.send(<message>, ack_callback);
Here, message
is the message that will be sent to the client and ack_callback
is the optional callback function that is invoked on the successful receipt of the message on the client.
A message or an event can be broadcasted to all the connected sockets using the broadcast
flag:
socket.broadcast.emit(<event>, <event_data>);
Here, event
is the name of event to emit and event_data
is the JSON data that will be sent with the event. The following line of code shows how to broadcast a message:
socket.broadcast.send(<message>);
Here, message
is the message that will be sent to the client and ack_callback
is the optional callback function that is invoked on the successful receipt of the message on the client.
Sometimes the message being sent is not important and can be ignored if not delivered. So these methods need not be queued or attempted to be redelivered. This is done with the volatile
flag:
socket.volatile.send(<message>);
Here, message
is the message that will be sent to the client and ack_callback
is the optional callback function that is invoked on the successful receipt of the message on the client.
We can call the set
method on the socket to store some data on the socket. This is an asynchronous method call and takes a key, value, and a callback function:
socket.set(<key>, <value>, function(){ //DO SOMETHING });
Here, key
is the key name for this data and value
is the value to store.
We use the get
method to fetch the value stored on a socket. This is an asynchronous method and takes a key and a callback function, which will get the value:
socket.get(<key>, function(value){ //DO SOMETHING });
Here, key
is the key of the data to fetch and value
is the value if stored with the socket. This will be null
if the value is not stored.
We can multiplex the socket and restrict messages/events to a namespace by using the of
method. This method returns a socket, which can be used as any other socket, but the messages will be restricted to only the clients connected to this namespace:
var namespace_socket = socket.of(<namespace>);
Here, namespace
is the namespace we want to restrict the socket to.
We use the join
method of socket
to join a room. It will create a new room if one doesn't already exist:
socket.join(<room>);
Here, room
is the name of the room to join.
We can send messages to all the
connected clients in the room by using the in
flag with broadcast
:
socket.broadcast.in(<room>).send(<message>); socket.broadcast.in(<room>).emit(<event>, <event_data>);
Here, room
is the room to send the message in, message
is the message to send, event
is the event to emit, and event_data
is the data to be sent with the event.
The leave
method is used to leave a room. We don't need to do this explicitly if the socket is exiting. Also, an empty room will automatically be destroyed:
socket.leave(<room>);
Here, room
is the room to exit from.
Socket.io is configured using the set
method in the configure
method's callback handler:
io.configure('environment', function () { io.set(<property>, <value>); });
Here, environment
is the optional environment in which this configuration will be used, property
is the property to be set, and value
is the value for the property.