IntroductionWhat Socket.IO isSocket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection. infoWebSocket is a communication protocol which provides a full-duplex and low-latency channel between the server and the browser. More information can be found here. There are several Socket.IO server implementations available:
And client implementations in most major languages:
Here's a basic example with plain WebSockets: Server (based on ws)
Client
And here's the same example with Socket.IO: Server
Client
Both examples looks really similar, but under the hood Socket.IO provides additional features that hide the complexity of running an application based on WebSockets in production. Those features are listed below. But first, let's make it clear what Socket.IO is not. What Socket.IO is notcautionSocket.IO is NOT a WebSocket implementation. Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.
If you are looking for a plain WebSocket server, please take a look at ws or µWebSockets.js. There are also discussions for including a WebSocket server in the Node.js core. On the client-side, you might be interested in the robust-websocket package. cautionSocket.IO is not meant to be used in a background service for mobile applications. The Socket.IO library keeps an open TCP connection to the server, which may result in a high battery drain for your users. Please use a dedicated messaging platform like FCM for this use case. FeaturesHere are the features provided by Socket.IO over plain WebSockets: HTTP long-polling fallbackThe connection will fall back to HTTP long-polling in case the WebSocket connection cannot be established. This feature was the #1 reason people used Socket.IO when the project was created more than ten years ago (!), as the browser support for WebSockets was still in its infancy. Even if most browsers now support WebSockets (more than 97%), it is still a great feature as we still receive reports from users that cannot establish a WebSocket connection because they are behind some misconfigured proxy. Automatic reconnectionUnder some particular conditions, the WebSocket connection between the server and the client can be interrupted with both sides being unaware of the broken state of the link. That's why Socket.IO includes a heartbeat mechanism, which periodically checks the status of the connection. And when the client eventually gets disconnected, it automatically reconnects with an exponential back-off delay, in order not to overwhelm the server. Packet bufferingThe packets are automatically buffered when the client is disconnected, and will be sent upon reconnection. More information here. AcknowledgementsSocket.IO provides a convenient way to send an event and receive a response: Sender
Receiver
You can also add a timeout:
BroadcastingOn the server-side, you can send an event to all connected clients or to a subset of clients:
This also works when scaling to multiple nodes. MultiplexingNamespaces allow you to split the logic of your application over a single shared connection. This can be useful for example if you want to create an "admin" channel that only authorized users can join.
More on that here. Common questionsIs Socket.IO still needed today?That's a fair question, since WebSockets are supported almost everywhere now. That being said, we believe that, if you use plain WebSockets for your application, you will eventually need to implement most of the features that are already included (and battle-tested) in Socket.IO, like reconnection, acknowledgements or broadcasting. What is the overhead of the Socket.IO protocol?
So, a few additional bytes for each message, which can be further reduced by the usage of a custom parser. infoThe size of the browser bundle itself is Something does not work properly, please help?Please check the Troubleshooting guide. Next steps |
|