Hulk
Hulk

Reputation: 1

what is the right syntax for node-redis PUBSUB CHANNELS

what is the right syntax for node-redis PUBSUB CHANNELS

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

I try like this but no message in console. When I try redis-cli console its working but I dont find right syntax in nodejs.

node-redis github: https://github.com/redis/node-redis

Upvotes: 0

Views: 67

Answers (1)

Simon Prickett
Simon Prickett

Reputation: 4158

Here's an example of how to use PUBSUB CHANNELS with node-redis 4.x:

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"
}

Subscribe to some channels in 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

Run the code:

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

Node version:

$ node --version
v18.14.2

Upvotes: 1

Related Questions