Skip to content
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

Add a note about multiple Pub/Sub channel listeners #2433

Merged
merged 2 commits into from
May 29, 2023

Conversation

jamesw6811
Copy link
Contributor

Clarify that multiple subscriptions create multiple listeners.

Description

This change informs the developer that multiple subscriptions will create multiple listeners. This is important because redis-cli SUBSCRIBE commands do not behave this way, so it may be counter-intuitive.
I personally spent some time debugging my code and my misunderstanding of this was the root cause.

Describe your pull request here
This changes the pub/sub docs to clarify that multiple subscriptions create multiple listeners.


Checklist

  • Does npm test pass with this change (including linting)?
  • Is the new or changed code fully tested?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?

Clarify that multiple subscriptions create multiple listeners.
@jamesw6811 jamesw6811 changed the title Add multiple listener language to pub/sub docks Add multiple listener language to pub/sub docs Mar 2, 2023
@leibale
Copy link
Collaborator

leibale commented Mar 2, 2023

@jamesw6811 Thanks for contributing, but... that's not true... when you SUBSCRIBE to the same channel more than once in redis-cli you will get the same message twice over the network, with node-redis you'll get the message once over the network, but both callbacks will be called:

// `SUBSCRIBE a`
await client.subscribe('a', message => {
  console.log(message);
});

// no command will be executed on the server
// but the callback will be added to the callback set
await client.subscribe('a', message => {
  console.log(message);
});

await anotherClient.publish('a', 'b');
// `client` will get the message once over the network, and then call both callbacks with the message.

@jamesw6811
Copy link
Contributor Author

jamesw6811 commented Mar 2, 2023

redis-cli stops accepting new commands after one SUBSCRIBE. I didn't realize this wasn't also the behavior of the api. It might confuse others, so I still think it's a helpful addition.

127.0.0.1:6379> subscribe hello
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "hello"
3) (integer) 1
subscribe hello     
subscribe hello

Redis docs:

Please note that when using redis-cli, in subscribed mode commands such as [UNSUBSCRIBE](https://redis.io/commands/unsubscribe) and [PUNSUBSCRIBE](https://redis.io/commands/punsubscribe) cannot be used because redis-cli will not accept any commands and can only quit the mode with Ctrl-C.

@jamesw6811
Copy link
Contributor Author

Maybe I'm not understanding the redis source correctly, but my reading is that the subscriptions are based on a dictionary of clients, so if the same client tried to subscribe twice it would not duplicate:
https://github.com/redis/redis/blob/7.0/src/pubsub.c#L228

Am I missing something? I'm newer to Redis so please excuse all the things I'm probably getting wrong...

@leibale
Copy link
Collaborator

leibale commented Mar 2, 2023

@jamesw6811 You can use telnet to do whatever you want...
Anyway, you are correct, even if you issue 2 SUBSCRIBE on the same channel you'll get the message once, I guess this made you expect the second listener to override the first one (and not just added on top)?

@leibale
Copy link
Collaborator

leibale commented Mar 2, 2023

@guyroyse :)

@jamesw6811
Copy link
Contributor Author

jamesw6811 commented Mar 2, 2023

@jamesw6811 You can use telnet to do whatever you want... Anyway, you are correct, even if you issue 2 SUBSCRIBE on the same channel you'll get the message once, I guess this made you expect the second listener to override the first one (and not just added on top)?

Exactly. Expecting either functionality is reasonable, but that documentation one-liner would have saved me some time. Thought it might save some others' time too.

Thanks for the telnet tip!

@leibale leibale changed the title Add multiple listener language to pub/sub docs Add a note about multiple Pub/Sub channel listeners Mar 3, 2023
@leibale leibale self-assigned this Mar 13, 2023
@leibale leibale merged commit 454617b into redis:master May 29, 2023
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants