Socket.IO is a library that enables low-latency, bidirectional, event-based
communication between a client and a server.
It is built on the WebSocket protocol and provides additional guarantees such as long HTTP polling or automatic reconnection.
There are several Socket.IO server implementations available
- Node.js) (documentation of which can be found here on this website)
- installation steps
- https://github.com/mrniko/netty-socketio Java
- Python: https://github.com/miguelgrinberg/python-socketio
- Golang: https://github.com/googollee/go-socket.io
source code: https://github.com/socketio/socket.io-deno Java:
E client implementations in most major languages
- that can be run in the browser, in Node.js or in React Native)
- installation steps
- source code
- (for WeChat applets): https://github.com/weapp-socketio/weapp.socket.io
- Java: https://github.com/socketio/socket.io-client-java
- C++: https://github.com/socketio/socket.io-client-cpp
- Swift: https://github.com/socketio/socket.io-client-swift
- Dart: https://github.com/rikulo/socket.io-client-dart
- Rust: https://github.com/1c3t3a/rust-socketio
- Kotlin: https://github.com/icerockdev/moko-socket-io
Here is a basic
example with WebSockets:Server
(based on ws)
And here is the same example with Socket.IO:
Both examples look very similar, but under the hood Socket.IO provides additional features that hide the complexity of running a WebSockets-based application in production. Those characteristics are listed below.
But first, let’s make clear what Socket.IO is not.
What Socket.IO is not
Although WebSocket Socket.IO used for transport when possible, it adds additional metadata to each package. That’s why a WebSocket client won’t be able to successfully connect to a Socket.IO server, and a Socket.IO client won’t be able to connect to a simple WebSocket server either.
If you’re looking for a simple WebSocket server, take a look at ws or μWebSockets.js.
There are also discussions to include a WebSocket server in the core .js Node.
On the client side, you might be interested in the robust-websocket package.
The library Socket.IO maintains an open TCP connection to the server, which can cause high battery consumption for users. Use a dedicated messaging platform such as FCM for this use case.
Features These are the features provided by Socket.IO over simple WebSockets:
long-probe HTTP The connection
will fall back to the long HTTP probe in case the WebSocket connection cannot be established.
This feature was the #1 reason people used Socket.IO when the project was created over ten years ago (!), as browser support for WebSockets was still in its infancy
Even if most browsers now support WebSockets (over 97%), it’s still a great feature, as we still get reports of users being unable to establish a WebSocket connection because they’re behind some bad proxy. Configured.
Under some particular conditions, the WebSocket connection between the server and the client may be interrupted without both parties being aware of the broken state
of the link.
That is why a heartbeat mechanism Socket.IO included, which periodically checks the status of the connection.
And when the client finally disconnects, it automatically reconnects with an exponential backspace delay, so as not to overwhelm the server.
Packets are automatically buffered when the client is disconnected and will be sent when the client is reconnected
More information here.
Socket.IO provides a convenient way to send an event
and receive a response
You can also add a timeout
On 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.
allow you to split application logic into 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.
Frequently Asked QuestionsStill
Socket.IO needed today?
That’s a fair question, since WebSockets are supported almost everywhere now
That said, we believe that if you use simple WebSockets for your application, you’ll eventually need to implement most of the features that are already included (and battle-tested) in Socket.IO, such as reconnect, acknowledgements, or streaming.
What is the overhead of the Socket.IO protocol?
socket.emit(“hello”, “world”) will be sent as a single WebSocket frame containing 42[“hello”,”world”] with:4 being Engine.IO type 2 “message” packet being
- Socket.IO packet type
- world”] being the JSON.stringify()-ed version of the argument
few additional bytes for each message, which can be further reduced by using a custom parser.
Something is not working properly, please help?
See the troubleshooting guide.
- Introduction example Server
- Client Installation