node-redis icon indicating copy to clipboard operation
node-redis copied to clipboard

what is the right syntax for node-redis PUBSUB CHANNELS

Open halilulker opened this issue 1 year ago • 1 comments

Description

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.

halilulker avatar Jul 28 '23 09:07 halilulker

Hi,

There are a few problems with your code:

  1. You are mixing Promises (await) and callbacks (the function you send as a second argument). See here for more details.
  2. The parentheses around ('PUBSUB CHANNELS', []) means that the 'PUBSUB CHANNELS' will be ignored, and only the empty array and the callback function will be passed to the sendCommand function:
function func() {
  console.log(arguments);
}

func((1, 2), 3);
// This will log [2, 3] and ignore 1
  1. You are sending PUBSUB CHANNELS as one argument instead of two (it should be ['PUBSUB', 'CHANNELS'], not ['PUBSUB CHANNELS']).

If you want to use the .sendCommand command to use the commands "as-is" do:

const result = await redis.sendCommand(['PUBSUB', 'CHANNELS']);

Or, if you want to use a "type-safe" more "JavaScript friendly" API (which is probably what your are looking for):

const result = await client.PUBSUB_CHANNELS();
// or
const result = await client.pubSubChannels();

leibale avatar Jul 31 '23 19:07 leibale