Skip to content

Instantly share code, notes, and snippets.

@kotobukid
Last active March 28, 2024 07:27
Show Gist options
  • Save kotobukid/3143f00f50ab3d2fecfa9bf2c93d7dff to your computer and use it in GitHub Desktop.
Save kotobukid/3143f00f50ab3d2fecfa9bf2c93d7dff to your computer and use it in GitHub Desktop.
1つのRedis接続インスタンスで複数のチャンネルをsubscribeできるか
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');
@kotobukid
Copy link
Author

publish hoge hogehoge
publish fuga fugafuga
publish aaa aaa

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