Aller au contenu principal

Socket.IO 3 Release

· 3 minutes de lecture
Damien Arrachequesne

We are happy to announce the release of Socket.IO v3!

Migration guide​

This release contains a few non backward compatible changes.

We have published a dedicated migration guide with all necessary details.

Why Socket.IO in 2020?​

That's an excellent question actually. Depending on your use case, it might make sense to use plain WebSocket directly:

What Socket.IO brings:

  • a fallback to HTTP long-polling, in case the WebSocket connection cannot be established

To be honest, this feature was awesome 10 years ago, when most browsers didn't support WebSocket, but this is not the case anymore. That's still a great safety net though.

  • auto-reconnection

Using plain WebSocket, you can take a look at robust-websocket.

  • a classic request-response API: acknowledgements
// on one side
socket.emit("updateitem", "1", { name: "updated" }, (response) => {
console.log(response.status); // ok
});
// on the other
socket.on("updateitem", (arg1, arg2, callback) => {
console.log(arg1); // 1
console.log(arg2); // { name: "updated" }
callback({
status: "ok"
});
});
  • a way to broadcast data to a given set of clients: Rooms
// server-side
io.on("connection", (socket) => {
socket.join("room1");

io.to("room1").emit("hello!");
});

Which also works when scaling to several Socket.IO servers (more information here).

  • a way to split your application logic into distinct modules (for more complex applications): Namespaces
// server-side
const adminNamespace = io.of("/admin");

adminNamespace.use((socket, next) => {
// ensure the socket is authorized
});

adminNamespace.on((socket) => {
socket.on("delete project", (id) => {
// in all handlers, we are sure that the socket is authorized
});
});

adminNamespace.emit("hello!"); // broadcast to admin only

What's next​

  • the Redis adapter (used when broadcasting across Socket.IO servers) will be updated in order to be compatible with Socket.IO v3 (update: done!)

  • the client implementations in other languages will be updated as well

  • a big focus on the documentation (additional code examples, extended guide, ...)

  • additional tooling around Socket.IO

Let's discuss!​

The project is now part of the beta of Github Discussions. Depending on the feedback of the community, it might replace the Slack channel in the future.

If you have any question about the release, let's discuss here.

Stay safe!