node-redis
node-redis copied to clipboard
what is the right syntax for node-redis PUBSUB CHANNELS
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.
Hi,
There are a few problems with your code:
- You are mixing
Promise
s (await
) and callbacks (the function you send as a second argument). See here for more details. - 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 thesendCommand
function:
function func() {
console.log(arguments);
}
func((1, 2), 3);
// This will log [2, 3] and ignore 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();