broadcaster icon indicating copy to clipboard operation
broadcaster copied to clipboard

Add redis backed with RedisPy

Open e-kondr01 opened this issue 2 years ago • 5 comments

Rationale

Broadcaster uses asyncio-redis in its RedisBackend. asyncio-redis is not actively maintained at the moment.

RedisPy (https://github.com/redis/redis-py) is the maintained Redis client for Python, and it has had AsyncIO support for quite some time.

Pull Request Content

This PR provides an updated Redis backend using RedisPy.

Problems faced

RedisPy doesn't allow listening for PubSub messages if the connection is not subscribed to any channels:

https://github.com/redis/redis-py/blob/master/redis/asyncio/client.py#L807

To solve this, the code of the Broadcaster class has been changed to start the listener task only after the first subscription to a channel.

Another solution would be to subscribe to an arbitrary channel in the connect method of Redis backend. I am not sure that I like this workaround.

However, I have not tested that these changes to the Broadcaster class do not break other backends.

e-kondr01 avatar May 05 '23 16:05 e-kondr01

hello

async def next_published(self) -> Event:
        message = None
        # get_message with timeout=None can return None
        while not message:
            #
            message = await self._sub_conn.get_message(
                timeout=None, ignore_subscribe_messages=True
            )
        return Event(
            channel=message["channel"].decode(),
            message=message["data"].decode(),
        )

here you need to use ignore_subscribe_messages = True

MahmudulHassan5809 avatar Oct 16 '23 21:10 MahmudulHassan5809

Is this gonna move forward?

MichalPham avatar Nov 15 '23 09:11 MichalPham

@MichalPham unfortunately I don't have the time right now to contribute to open source. This MR is a demonstration of how I've implemented RedisPy backend to Broadcaster in my work project, so interested people could use it as a reference or a starting point.

e-kondr01 avatar Nov 15 '23 11:11 e-kondr01

@encode/maintainers I fully support transition to redis-py. I will take a look at this PR soon.

alex-oleshkevich avatar Nov 15 '23 13:11 alex-oleshkevich

@encode/maintainers I fully support transition to redis-py. I will take a look at this PR soon.

Whatever you think is fine by me.

Kludex avatar Nov 15 '23 14:11 Kludex