The client

In this section we will get to know the client APIs.

Connecting to a socket

We connect to a socket using the connect method on the io object in the client:

var socket = io.connect(<uri>);

Here, uri is the server URI to connect to. It can be absolute or relative. If it is other than /, or an absolute equivalent of that, it will connect to the namespace.

Listening to events

We can attach event handlers to a socket using the on method:

socket.on(<event>, function(event_data, ack_callback){});

Here, event is the event to listen for, event_data is the data for the event, and ack_callback is the optional callback method to call to acknowledge the receipt of the event.

Emitting an event

We use the emit method to trigger an event. This event will be handled on the server:

socket.on(<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 message on the server.

Sending a message

The send method is used to send a message to the server:

socket.send(<message>, ack_callback);

Here, message is the message that will be sent to the server and ack_callback is the optional callback function that is invoked on the successful receipt of the message on the server.

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

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