Skip to content

Conversation

@seta-chuang-0909
Copy link
Contributor

Summary

This PR adds support for using the sharded adapter with ioredis Cluster, which was previously not possible due to ioredis#1759.

Starting with [email protected], the sharded Pub/Sub functionality has been fixed via ioredis#1956, enabling proper routing of SSUBSCRIBE/SPUBLISH commands in cluster mode.

Changes

New Features

  • Added isIoRedisCluster() helper function in lib/util.ts to detect ioredis Cluster instances
  • Added hasShardedSubscribers() helper function in lib/util.ts to check if the shardedSubscribers option is enabled
  • Added validation in ShardedRedisAdapter constructor to ensure users enable the shardedSubscribers option when using ioredis Cluster

Documentation

  • Updated README.md with usage instructions for ioredis Cluster with sharded Pub/Sub
  • Added table of contents entry for the new section

Tests

  • Enabled the previously skipped [sharded] ioredis cluster test
  • Added shardedSubscribers: true option to the test configuration
  • Added natMap configuration for Docker environment compatibility

Dependencies

  • Updated ioredis dev dependency from ^5.3.2 to ^5.9.0

Usage

import { Cluster } from "ioredis";
import { Server } from "socket.io";
import { createShardedAdapter } from "@socket.io/redis-adapter";

const pubClient = new Cluster(
  [
    { host: "localhost", port: 7000 },
    { host: "localhost", port: 7001 },
    { host: "localhost", port: 7002 },
  ],
  {
    shardedSubscribers: true, // Required for sharded Pub/Sub
  }
);
const subClient = pubClient.duplicate();

const io = new Server({
  adapter: createShardedAdapter(pubClient, subClient)
});

io.listen(3000);

# Breaking Changes
None. This is a new feature that enables previously unsupported functionality.

# Related Issues
Fixes the limitation mentioned in the README: "it is not currently possible to use the sharded adapter with the ioredis package and a Redis cluster"
Reference: https://github.com/redis/ioredis/pull/1956

- 在 util.ts 中添加 isIoRedisCluster 和 hasShardedSubscribers 輔助函數
- 在 sharded-adapter.ts 中添加 ioredis Cluster 配置驗證
- 啟用 [sharded] ioredis cluster 測試(需要 shardedSubscribers: true)
- 更新 package.json 中 ioredis 版本要求到 ^5.9.0
- 更新 README.md 添加 ioredis Cluster 與 sharded pub/sub 的使用說明
- 添加 ioredis Cluster 的 natMap 配置以解決 Docker 環境問題

參考: redis/ioredis#1956
darrachequesne pushed a commit that referenced this pull request Jan 20, 2026
@darrachequesne
Copy link
Member

Merged as c2d668b. Thanks a lot 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants