how does socket.io work? [closed] how does socket.io work? [closed] javascript javascript

how does socket.io work? [closed]


For debugging, you might want to try out Theseus.

Here is a short overview of the socket.io SPEC:

Socket.IO aims to bring a WebSocket-like API to many browsers and devices, with some specific features to help with the creation of real-world realtime applications and games.

  • Multiple transport support (old user agents, mobile browsers, etc).
  • Multiple sockets under the same connection (namespaces).
  • Disconnection detection through heartbeats.
  • Optional acknoledgments.
  • Reconnection support with buffering (ideal for mobile devices or bad networks)
  • Lightweight protocol that sits on top of HTTP.

Anatomy of a Socket.IO socket

A Socket.IO client first decides on a transport to utilize to connect.

The state of the Socket.IO socket can be disconnected, disconnecting, connected and connecting.

The transport connection can be closed, closing, open, and opening.

A simple HTTP handshake takes place at the beginning of a Socket.IO connection. The handshake, if successful, results in the client receiving:

  • A session id that will be given for the transport to open connections.
  • A number of seconds within which a heartbeat is expected (heartbeat timeout)
  • A number of seconds after the transport connection is closed when the socket is considered disconnected if the transport connection is not reopened (close timeout).

At this point the socket is considered connected, and the transport is signaled to open the connection.

If the transport connection is closed, both ends are to buffer messages and then frame them appropriately for them to be sent as a batch when the connection resumes.

If the connection is not resumed within the negotiated timeout the socket is considered disconnected. At this point the client might decide to reconnect the socket, which implies a new handshake.

If you need more of the details, you can read the rest of the specification here


JAM's post does a good job of summarizing what socket.io is; I'd like to specifically address some of your other questions.

  • Socket.io attaches to an instance of http.Server and adds handlers to it. It does not listen to a network port on its own; it simply adds socket.io-specific handlers to an existing HTTP server. (However, if you call io.listen() with a number, it internally creates a new HTTP server that listens to the specified port and attaches to that.)

  • It really stays open if it is using the WebSockets transport. It also includes fallback mechanisims that use traditional (long-)polling ajax requests. So the answer depends on what APIs the browser supports. (You can optionally configure which fallbacks you'd like to use, if any.)

  • Fiddler supports websockets now, as does Chrome's developer tools:

enter image description here