Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does socket.io work? [closed]

I am using socket.io and it was quick to setup (thanks to examples on their usage page) but i'd like to find out more about what exactly is going on under covers and what's the technology that makes it work.

what are the exact mechanics of socket.io?

is it on port 80 or a separate one?

does it really stay open or is that part simulated?

is there a way to profile each socket event ? (sort of like using fiddler to see what happens in ajax calls)

like image 260
Sonic Soul Avatar asked May 23 '13 16:05

Sonic Soul


People also ask

How does Socket.IO work internally?

Socket.IO allows bi-directional communication between client and server. Bi-directional communications are enabled when a client has Socket.IO in the browser, and a server has also integrated the Socket.IO package. While data can be sent in a number of forms, JSON is the simplest.

Does Socket.IO reconnect automatically?

In the first case, the Socket will automatically try to reconnect, after a given delay.

Does Socket.IO have a timeout?

So, you can configure both the server and client side timeout settings by setting the following timeout properties on the engine.io component inside of socket.io.


2 Answers

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

like image 136
JAM Avatar answered Oct 06 '22 00:10

JAM


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

like image 31
josh3736 Avatar answered Oct 06 '22 00:10

josh3736