Skip to content

Error: Cannot send commands in PubSub mode when using redis server with password #2332

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ngocson952006 opened this issue Nov 22, 2022 · 4 comments
Labels

Comments

@ngocson952006
Copy link

ngocson952006 commented Nov 22, 2022

Using the Pub/Sub feature described at https://redis.js.org/#node-redis-usage-pubsub works fine for me when using a Redis server without a password set.

But when changing connect to a Redis server using a password for authentication, I got the error Cannot send commands in PubSub mode (Despite that the connection was successful). Details below

Error: Cannot send commands in PubSub mode
    at RedisCommandsQueue.addCommand (/.../node_modules/@redis/client/dist/lib/client/commands-queue.js:72:35)
    at RedisSocket.socketInitiator (/.../node_modules/@redis/client/dist/lib/client/index.js:337:81)
    at RedisSocket._RedisSocket_connect (/.../node_modules/@redis/client/dist/lib/client/socket.js:140:81)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async Commander.connect (/.../node_modules/@redis/client/dist/lib/client/index.js:167:9)

Process finished with exit code 1

I'm not sure if this is duplicated with #2080 but the version of the lib I'm using is v4.4.0.

Environment:

  • Node.js Version: v16.13.1
  • Redis Server Version: Redis server v=6.2.4
  • Node Redis Version: v4.4.0
  • Platform: Mac OS 13.0.1
@leibale
Copy link
Contributor

leibale commented Nov 22, 2022

@ngocson952006 Hi, can you please provide a code example that reproduces the issue? because this code didn't:

import { createClient } from '@redis/client';

const client = createClient({
  username: 'default',
  password: '123'
});

client.on('error', err => console.error(err));

await client.connect();

await client.subscribe('test', (message) => {
  console.log(message);
});

console.log('Restart redis server now');

edit: maybe it was fixed in 4.5.0? 🤔

@ngocson952006
Copy link
Author

ngocson952006 commented Nov 23, 2022

Thanks, @leibale for your response. Honestly, I can not share the code because it's from my work project. But I can explain follow I that I encountered the error.

My project uses Node-Redis to read, and write Redis data and also uses Pub/Sub for notification handling.
First, I create a client for reading and writing data.

import { createClient } from '@redis/client';
const client = createClient({
  url: process.env.REDIS_URL
});
client.on('error', err => console.error(err));
await client.connect();

We use the URL to connect to Redis-Server and save this to the environment variable. The pattern of URL is followed by redis[s]://[[username][:password]@][host][:port][/db-number]. This works fine for both cases Redis without a password and Redis with a password

Then, from another business logic class, I want to use Pub/Sub with the same URL connection. I created a duplicate connection.

pubSubRedisClient = client.duplicate();
await pubSubRedisClient.connect();
await pubSubRedisClient.subscribe('channel', (message) => {
logger.info("Received message: " + message);}, true);

At this point, I have a problem. The PubSub code works fine for the case of Redis without a password(example URL: redis://localhost:16379/0). In case Redis with password (example URL: redis://:password_123@localhost:6379/0), the code raises the error Cannot send commands in PubSub mode

@bmiller59
Copy link

@ngocson952006 I had the same issue and i realized that I was not awaiting on the redis client connection before calling the subscribe() function. Make sure to await the connection first and I believe the issue will be resolved.

@ngocson952006
Copy link
Author

@ngocson952006 I had the same issue and i realized that I was not awaiting on the redis client connection before calling the subscribe() function. Make sure to await the connection first and I believe the issue will be resolved.

Thanks @bmiller59, you saved my day. I need to wait for all clients(including the duplicated client) to be connected first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants