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 redis pubClient and subClient, 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 the io function is changed:
- const io = require('socket.io-client');

+ const { io } = require('socket.io-client');

参考文献

javascript socket.io nodejs