node-redis PUBSUB CHANNELS的正确语法是什么

soat7uwm  于 2023-08-02  发布在  Redis
关注(0)|答案(1)|浏览(270)

node-redis PUBSUB CHANNELS的正确语法是什么

await redis.sendCommand(('PUBSUB CHANNELS', []), function(err,result){
    console.log(err);
    console.log(result);
  });

字符串
我尝试这样做,但没有消息在控制台。当我尝试redis-cli控制台它的工作,但我没有找到正确的语法在nodejs。
node-redis github:https://github.com/redis/node-redis

zzwlnbp8

zzwlnbp81#

下面是如何在node-redis 4.x中使用PUBSUB CHANNELS的示例:
index.js:

import { createClient } from 'redis';

const client = createClient();

await client.connect();

const activeChannels = await client.pubSubChannels();
console.log(activeChannels);

await client.quit();

字符串
package.json:

{
  "name": "nrpubsubchannels",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "redis": "^4.6.7"
  },
  "type": "module"
}


订阅redis-cli中的一些频道:

$ redis-cli
127.0.0.1:6379> subscribe channel1 channel2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "subscribe"
2) "channel2"
3) (integer) 2


运行代码:

$ node index.js
[ 'channel2', 'channel1' ]


节点版本:

$ node --version
v18.14.2

相关问题