Last active
March 28, 2024 07:27
-
-
Save kotobukid/3143f00f50ab3d2fecfa9bf2c93d7dff to your computer and use it in GitHub Desktop.
1つのRedis接続インスタンスで複数のチャンネルをsubscribeできるか
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Redis from "ioredis"; | |
const redis = new Redis(); | |
redis.on('message', (ch: string, mes: string): void => { | |
console.log({ | |
ch, | |
mes | |
}); | |
}); | |
setTimeout((): void => { | |
console.log('aaa購読開始'); | |
redis.subscribe('aaa'); | |
redis.subscribe('aaa'); // 重複サブスクライブ指示でもイベントがダブりはしない(問題なし) | |
}, 2000); | |
setTimeout((): void => { | |
console.log('hoge停止') | |
redis.unsubscribe('hoge'); | |
setTimeout(() => { | |
console.log('hoge再開') | |
redis.subscribe('hoge'); | |
}, 5000); | |
}, 5000); | |
redis.unsubscribe('----'); // サブスクライブしてもいないチャンネルをアンサブスクライブしても問題は発生しない | |
redis.subscribe('hoge', 'fuga'); |
Author
kotobukid
commented
Mar 28, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment