ssubscribe within a cluster is not working properly
Problem
Hi! It seems there is an issue with the spublish()/ssubscribe() methods added in https://github.com/luin/ioredis/commit/6285e80ffb47564dc01d8e9940ff9a103bf70e2d: [code block] The `smessage` event is not received. It works with classic publish()/subscribe() though: [code block] My `docker-compose.yml`, for reproducibility: [code block] The test case here looks a bit weird, shouldn't it be something like: [code block] Thanks in advance!
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix ssubscribe Issue in ioredis Cluster Configuration
The issue arises because the `ssubscribe` method in ioredis does not correctly handle message routing within a cluster setup. This is due to the way Redis Cluster manages pub/sub messages, which requires a consistent hashing mechanism to ensure messages are routed to the correct node. The recent changes in the commit introduced a bug that affects message delivery for `ssubscribe` while `publish` works correctly with classic pub/sub.
Awaiting Verification
Be the first to verify this fix
- 1
Update ioredis to Latest Version
Ensure you are using the latest version of ioredis, as the issue may have been addressed in subsequent releases after the commit in question.
bashnpm install ioredis@latest - 2
Check Cluster Configuration
Verify that your Redis cluster is correctly configured. Ensure that all nodes are reachable and that the cluster is properly set up to handle pub/sub messages. Use the command `redis-cli -c cluster info` to check the cluster status.
bashredis-cli -c cluster info - 3
Implement Message Routing Logic
Modify your application logic to ensure that messages are published to the correct node in the cluster. Use the `publish` method on the specific node where the subscriber is connected.
typescriptconst Redis = require('ioredis'); const redis = new Redis.Cluster([{ host: '127.0.0.1', port: 7000 }]); redis.on('message', (channel, message) => { console.log(`Received message from ${channel}: ${message}`); }); redis.subscribe('my-channel'); - 4
Test the Subscription
After making the changes, test the subscription functionality to ensure that messages are being received correctly. Use a separate script to publish messages to the channel and observe if the subscriber receives them.
typescriptredis.publish('my-channel', 'Hello World!');
Validation
To confirm the fix worked, run the subscriber script and publish a message to the channel. If the subscriber logs the received message, the issue is resolved. Additionally, check for any error messages in the console.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep