What Engine.IO brings to the table?
1. Handshake
At the beginning of the Engine.IO connection, the server sends some information:
{
"sid": "FSDjX-WRwSA4zTZMALqx",
"upgrades": ["websocket"],
"pingInterval": 25000,
"pingTimeout": 20000
}
- the
sidis the ID of the session, it must be included in thesidquery parameter in all subsequent HTTP requests - the
upgradesarray contains the list of all "better" transports that are supported by the server - the
pingIntervalandpingTimeoutvalues are used in the heartbeat mechanism
2. Upgrade mechanism
By default, the client establishes the connection with the HTTP long-polling transport.
But, why?
While WebSocket is clearly the best way to establish a bidirectional communication, experience has shown that it is not always possible to establish a WebSocket connection, due to corporate proxies, personal firewall, antivirus software...
From the user perspective, an unsuccessful WebSocket connection can translate in up to 10 seconds of waiting for the realtime application to begin exchanging data. This hurts user experience.
To summarize, Engine.IO focuses on reliability and user experience first, and marginal potential UX improvements and increased server performance second.
To upgrade, the client will:
- ensure its outgoing buffer is empty
- put the current transport in read-only mode
- try to establish a connection with the other transport
- if successful, close the first transport
3. Disconnection detection
The Engine.IO connection is considered as closed when:
- one HTTP request (either GET or POST) fails (for example, when the server is shutdown)
- the WebSocket connection is closed (for example, when the user closes the tab in its browser)
socket.disconnect()is called on the server-side or on the client-side
There is also a heartbeat mechanism which checks that the connection between the server and the client is still up and running.
4. Heartbeat mechanism
- At a given interval (the
pingIntervalvalue sent in the handshake) the server sends a PING packet. - The client has a few seconds (the
pingTimeoutvalue) to send a PONG packet back. - If the server does not receive a PONG packet back, it will consider that the connection is closed.
- Conversely, if the client does not receive a PING packet within
pingInterval + pingTimeout, it will consider the connection as closed.