Appendix A. Socket.IO Quick Reference

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.

The server

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.

Instantiating socket

The socket.io module is instantiated, just like any other node module, by using require to import the module:

var io = require('socket.io'),

Starting 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.

Listening to events

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.

Emitting an event

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.

Sending a message

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.

Sending a JSON message

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.

Broadcasting a message/event

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.

Sending a volatile message

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.

Storing socket data

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.

Getting the socket data

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.

Restricting to a namespace

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.

Joining a room

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.

Broadcasting messages/events in a room

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.

Leaving a room

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.

Changing the configuration

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset