basiliscos / cpp-bredis

Boost::ASIO low-level redis client (connector)
MIT License
147 stars 36 forks source link

subscriptions to multiple channels issue #47

Closed silviuzil closed 3 years ago

silviuzil commented 3 years ago

Hi,

Thanks for a great library, very useful.

I have an issue with multiple subscriptions. One way of subscribing is (following the documentation): bredis::single_command_t subscribe_cmd{ "subscribe", "channel-1", "channel-2" }; Then one can validate the subscription using: bredis::marker_helpers::check_subscription<Iterator> check_subscription{std::move(subscribe_cmd)}; ...; // get the 1st reply auto parse_result = ...; bool channel_1_ok = boost::apply_visitor(check_subscription, parse_result.result); ...; // get the 2nd reply parse_result = ...; bool channel_2_ok = boost::apply_visitor(check_subscription, parse_result.result);

However, if subscriptions are not simultaneous, that is we subscribe to channel-1 and later subscribe to channel-2, the validation fails on channel-2: // get the 2nd reply parse_result = ...; bool channel_2_ok = boost::apply_visitor(check_subscription, parse_result.result);

The reason is that redis response has only 3 fields and the value in the 3rd field is 2, which doesn't comply with the validation tests in check_subscription.

Any work around?

Thanks, Silviu

basiliscos commented 3 years ago

I think you misuse redis API: once you entered into the subscription

When a Redis client subscribes to a Pub/Sub channel, the protocol changes semantics and becomes a push protocol, that is, the client no longer requires sending commands, because the server will automatically send to the client new messages (for the channels the client is subscribed to) as soon as they are received.

See, https://redis.io/topics/protocol

In other words there is a problem to know, is it a new message or subscription confirmation to channel-2.

Easy workaround : just don't validate (usually it is fine), or do manual inspection of each new redis message, until u get all confirmation, as it might be:

confirm-channel1, msg-channel-1, msg-channel-1, ..., msg-channel-1, confirm-channel-2
silviuzil commented 3 years ago

Thanks @basiliscos , I did the manual validation. Just wanted to make sure I didn't miss a hidden feature.