将 socket.io 从 v3 升级到 v4
继 将 socket.io 从 v2 升级到 v3 之后,本文是关于将 socket.io
及其相关软件包升级到撰写本文时的最新版本,唯一的设置更改似乎是来自 redis-adapater
和这次导入客户端 io
的方式(针对我们的特定用例)。
Upgrade guide
Basically read the Migrating from 3.x to 4.0 official guide, then:
- For npm packages
- "socket.io": "3.1.2",
- "socket.io-emitter": "3.2.0",
- "socket.io-redis": "6.0.1",
+ "socket.io": "4.4.1",
+ "@socket.io/redis-emitter": "4.1.1",
+ "@socket.io/redis-adapter": "7.1.0",
- For redis-adapter config
The adapter has changed method signatures about the redispubClient
andsubClient
, see this issues/411 for details.
- this.io.adapter(
- createAdapter({
- pubClient,
- subClient,
- }),
- );
+ this.io.adapter(
+ createAdapter(pubClient, subClient),
+ );
- For client (if any)
The way to import theio
function is changed:
- const io = require('socket.io-client');
+ const { io } = require('socket.io-client');
参考资料
- socket.io 4.x documentation
- socket.io 4.x server api documentation
- socket.io 4.x client api documentation