Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ The HTML template for the front end [is available here](https://github.com/encod

* `Broadcast('memory://')`
* `Broadcast("redis://localhost:6379")`
* `Broadcast("redis-stream://localhost:6379")`
* `Broadcast("postgres://localhost:5432/hostedapi")`

## Where next?
Expand All @@ -87,6 +88,6 @@ state, make sure to strictly pin your requirements to `broadcaster==0.1.0`.
To be more capable we'd really want to add some additional backends, provide API support for reading recent event history from persistent stores, and provide a serialization/deserialization API...

* Serialization / deserialization to support broadcasting structured data.
* Backends for Redis Streams, Apache Kafka, and RabbitMQ.
* Backends for Apache Kafka, and RabbitMQ.
* Add support for `subscribe('chatroom', history=100)` for backends which provide persistence. (Redis Streams, Apache Kafka) This will allow applications to subscribe to channel updates, while also being given an initial window onto the most recent events. We *might* also want to support some basic paging operations, to allow applications to scan back in the event history.
* Support for pattern subscribes in backends that support it.
43 changes: 43 additions & 0 deletions broadcaster/_backends/redis_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import aioredis
import asyncio
import typing

from .base import BroadcastBackend
from .._base import Event


class RedisStreamBackend(BroadcastBackend):
def __init__(self, url: str):
self.conn_url = url.replace('redis-stream', 'redis', 1)
self.streams: typing.Set = set()

async def connect(self) -> None:
loop = asyncio.get_event_loop()
self._producer = await aioredis.create_redis(self.conn_url, loop=loop)
self._consumer = await aioredis.create_redis(self.conn_url, loop=loop)

async def disconnect(self) -> None:
self._producer.close()
self._consumer.close()

async def subscribe(self, channel: str) -> None:
self.streams.add(channel)

async def unsubscribe(self, channel: str) -> None:
await self.streams.discard(channel)

async def publish(self, channel: str, message: typing.Any) -> None:
await self._producer.xadd(channel, {'message': message})

async def _wait_for_streams(self) -> None:
while not self.streams:
await asyncio.sleep(1)

async def next_published(self) -> Event:
await self._wait_for_streams()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I want to wait for at least one stream.
Don't know is it a good approach or not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't it work fine without the sleep? It should only stop reading if timeout is specified. I'll check it tomorrow

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it's a bit complicated. For instance, we also want to properly cover the case where a new stream is subscribed too, while we're waiting on an existing xread.

messages = await self._consumer.xread(list(self.streams))
stream, _msg_id, message = messages[0]
return Event(
channel=stream.decode('utf-8'),
message=message.get(b'message').decode('utf-8'),
)
4 changes: 4 additions & 0 deletions broadcaster/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def __init__(self, url: str):
from ._backends.redis import RedisBackend
self._backend = RedisBackend(url)

elif parsed_url.scheme == 'redis-stream':
from ._backends.redis_stream import RedisStreamBackend
self._backend = RedisStreamBackend(url)

elif parsed_url.scheme in ('postgres', 'postgresql'):
from ._backends.postgres import PostgresBackend
self._backend = PostgresBackend(url)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_broadcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ async def test_redis():
assert event.message == 'hello'


@pytest.mark.asyncio
async def test_redis_stream():
async with Broadcast('redis-stream://localhost:6379') as broadcast:
async with broadcast.subscribe('chatroom') as subscriber:
await broadcast.publish('chatroom', 'hello')
event = await subscriber.get()
assert event.channel == 'chatroom'
assert event.message == 'hello'


@pytest.mark.asyncio
async def test_postgres():
async with Broadcast('postgres://localhost:5432/hostedapi') as broadcast:
Expand Down