跳到主要内容

Socket.IO 4.4.0

· 2 分钟阅读
Damien Arrachequesne

Hello everyone!

We have just published a new minor version of Socket.IO: 4.4.0

After a big focus on the client in the latest release, this release is more oriented towards the server.

Support for µWebSockets.js

Why should one choose between performance and reliability? Starting with socket.io@4.4.0, you can now use the HTTP/WebSocket server provided by the µWebSockets.js package:

const { App } = require("uWebSockets.js");
const { Server } = require("socket.io");

const app = new App();
const io = new Server();

io.attachApp(app);

io.on("connection", (socket) => {
// ...
});

app.listen(3000, (token) => {
if (!token) {
console.warn("port already in use");
}
});

It acts as a replacement for both the default Node.js HTTP server and the WebSocket server provided by the ws package, and should improve the memory footprint and the latency of your application. Benchmarks coming soon!

Emit with timeout

There is a new socket.timeout() method available on both sides:

socket.timeout(5000).emit("my-event", (err) => {
if (err) {
// the other side did not acknowledge the event in the given delay
}
});

socket.data can now be typed

For TypeScript users, the socket.data can now be typed:

interface SocketData {
name: string;
age: number;
}

const io = new Server<ClientToServerEvents, ServerToClientEvents, InterServerEvents, SocketData>();

io.on("connection", (socket) => {
socket.data.name = "john";
socket.data.age = 42;
});

This release also includes some bug fixes, please see the complete diff below.

Size of the bundles:

minmin+gzip
socket.io.min.js40.4 KB (+ 0.4 KB ⬆️)13.1 KB (+ 0.1 KB ⬆️)
socket.io.msgpack.min.js45.6 KB (+ 0.4 KB ⬆️)14.2 KB (-)
socket.io.esm.min.js33.1 KB (+ 0.3 KB ⬆️)11.3 KB (+ 0.2 KB ⬆️)

That's all for this release, thanks for reading!