Ir para o conteúdo principal

Monthly update #3

· 3 minutes de lecture
Damien Arrachequesne

Hi everyone!

Here's the #3 edition of our Monthly update.

So, what's new in the Socket.IO ecosystem?

Socket.IO v4

Wait, already v4?

Yes, but the upgrade should be a lot easier than from v2 to v3, since the two breaking changes only impact the API on the server side.

The most important breaking change is related to broadcasting: calling to() or any other broadcast modifier will now return an immutable object.

Previously, the modifiers were saved on the io instance directly, which could lead to surprising behaviors:

io.to("room1");
// and then later
io.to("room2").emit(/* ... */); // also sent to room1

You were also not able to reuse the operator:

const operator = io.to("room1");

operator.emit(/* ... */); // to room1 (assuming synchronous call)
operator.emit(/* ... */); // to all clients

Even more surprising, with asynchronous code:

io.to("room3").emit("details", await fetchDetails()); // unspecified behavior: maybe in room3, maybe to all clients

Starting with v4.0.0, you can now use io.to() safely:

const operator = io.to("room1").except("room2");

operator.emit(/* ... */);
operator.emit(/* ... */);

This release also brings some interesting new features, like the fetchSockets() method.

Please check the migration guide here.

Redis Adapter v6.1.0 and Redis Emitter v4.0.0

The Redis adapter (for broadcasting packets with multiple Socket.IO servers) and the Redis emitter (for broadcasting packets from another Node.js process) have been updated in order to support the new features of the v4 release.

For example, the socketsJoin method will work across Socket.IO servers:

// make all Socket instances join the "room1" room
io.socketsJoin("room1");

// make all Socket instances in the "room1" room join the "room2" and "room3" rooms
io.in("room1").socketsJoin(["room2", "room3"]);

// make all Socket instances in the "room1" room of the "admin" namespace join the "room2" room
io.of("/admin").in("room1").socketsJoin("room2");

More information can be found here.

Documentation

The documentation continues to be improved.

A new example project has been added in the "Get started" section, with middlewares and rooms: Private messaging

As usual, if you find a typo or think that some details are missing, please open an issue here: https://github.com/socketio/socket.io-website

New versions

What's next

  • a continuous focus on the documentation (additional code examples, extended guide, ...)
  • additional tooling around Socket.IO

Stay safe!