how does socket.io work?

Javascriptnode.jssocket.io

Javascript Problem Overview


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)

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSonic SoulView Question on Stackoverflow
Solution 1 - JavascriptJAMView Answer on Stackoverflow
Solution 2 - Javascriptjosh3736View Answer on Stackoverflow