scredis / scredis

Non-blocking, ultra-fast Scala Redis client built on top of Akka IO.

Home Page:https://scredis.github.io/scredis/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Multiple subscription / psubscriptions

mikwys opened this issue · comments

Hi,
I am not sure if this is an issue or I am using subscriptions in a wrong way.
When trying to subscribe and psubscribe using subscriber, only recent attached Subscription (PartialFunction) is used as callback. This makes sense having in mind that this gets overwritten:
https://github.com/scredis/scredis/blob/master/src/main/scala/scredis/io/SubscriberListenerActor.scala#L112

I've ended up with global partial function for all subscriptions, but I need to pass it as last one, so clearly it looks like a workaround.

r.subscriber.pSubscribe(subscribeTo: _*){PartialFunction.empty} 
r.subscriber.subscribe("sub-channel")(subscriptionFunction) //for all

Maybe partial function should be somehow composed, however this will surely complicate other functinos like "unsubscribe".

I was also tripped up by this. I expected each call to subscribe to register each partial function with the channel(s) subscribed to. The docs to not make this behavior clear.

Joining a club of 'I was also tripped up by this'.
This pf also handles Subscribe/Unsubscribe/PSubscribe/PUnsubscribe actions which are not directly related to subscription itself which means using it per subscription might be confusing too.
This pf function should be provided for SubscriberClient creation and if you are interested in different handlers for different channels it should be done in code handling Message / PMessage in client code in pf function.

Maybe also a little wrapper with mentioned logic could be useful in scredis to provide minimal example of how it can be achieved but it would have to be on top of scredis handling logic itself.

I changed this pf to be Function: PubSubMessage -> Unit which you provide during Redis instance creation. This way it is clear that it will be applied to everything you subscribe to and process all events from different subscriptions. That means you no longer need to provide anything during subscribe/unsubscribe. Also documentation was updated to reflect those changes.

Because we have api breaking changes new version will have major number increased.

As mentioned before different logic for messages from different channels must be done in client code inside Subscription. If there is a need maybe a little wrapper can be added to handle such cases but only if there is a real need for that and willingness to work on that.

Closing this issue, if you have any idea how to improve this pub/sub even further please open new issue.

Related PR: https://github.com/scredis/scredis/pull/77/files