The Socket.IO connection

The socket.io connection begins with the handshake. This makes the handshake a special part of the protocol. Apart from the handshake, all the other events and messages in the protocol are transferred over the socket.

Socket.io is intended for use with web applications, and therefore it is assumed that these applications will always be able to use HTTP. It is because of this reasoning that the socket.io handshake takes place over HTTP.

To initiate the connection and hence perform the handshake, the client performs a POST request on the handshake URI (built from the URI passed to the connect method). Let us take the same socket.io connection URI and try to understand its various parts. Let us say that the URI is as follows:

http://myhost.com:8080/socket.io/1/

Let us break down and understand this URI.

http is the protocol being used. We can set it to use https, using https in the client's connect ct method.

myhost.com again comes from the connect method and is the name or IP address of the host you want to connect to. The default i localhost.

8080 is the port over which your server is listening. This is also passed to the connect method when we are invoking it. The default is 80.

socket.io is the namespace that handles all the connect requests.

1 is the socket.io protocol version number.

The server can respond to this in one of thee three ways:

  • 200 OK – This will be the server's response when the handshake is successful. In addition to the status, the body of the response should be a colon-separated list of the session ID given to this connection, the heartbeat timeout, the connection closing timeout, and the list of supported transports separated by commas. A sample response body looks like this:
    8749dke9387:20:10:websocket,flashsocket,xhr-polling
  • 401 UnauthorizedThis will be the response from the server in case the authorization handler fails to authorize the client. As we saw in the previous chapter, this is the handler we attach to the authorize event on the server, and it uses the connection and cookie information to authorize the user.
  • 503 Service Unavailable – When the server has any other reason, including errors, to deny service to the client.

If the handshake is successful, based on the transports provided by the server and the one supported by the client the socket.io client will start communicating with the server on a particular URI. This URI has the form [scheme]://[host]/[namespace]/[version]/[transportId]/[sessionId].

  • [scheme] is the protocol the client will be using to communicate. In the case of WebSockets, this is either ws or wss, while in the case of XHR, it is either http or https.
  • [host] is the server name or IP Address.
  • [namespace] is the socket.io namespace we want to send the message to.
  • [version] is the version of the socket.io protocol that we are using, currently 1.
  • [transportId] is the the name of the transport mechanism chosen for the communication.
  • [sessionId] is the session ID given to the client by the server during the handshake.

In the case of bidirectional transport, such as WebSocket, the connection opened at this URI will be used to send and receive messages.

For unidirectional transports such as XHR long polling, the client will perform a GET request on this URI, which the server will keep on hold till it has some data to send, while the client will perform a POST request on this URI whenever it has to send a message or an event to the server.

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

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