Skip to main content

Simple broadcast channels.

Project description

Broadcaster

Broadcaster helps you develop realtime streaming functionality in by providing a simple broadcast API onto a number of different backend services.

It currently supports Redis PUB/SUB, and Postgres LISTEN/NOTIFY, plus a simple in-memory backend, that you can use for local development or during testing.

Here's a complete example of the backend code for a simple websocket chat app:

app.py

# Requires: `starlette`, `uvicorn`, `jinja2`
# Run with `uvicorn example:app`
from broadcaster import Broadcast
from starlette.applications import Starlette
from starlette.concurrency import run_until_first_complete
from starlette.routing import Route, WebSocketRoute
from starlette.templating import Jinja2Templates


broadcast = Broadcast("redis://localhost:6379")
templates = Jinja2Templates("templates")


async def homepage(request):
    template = "index.html"
    context = {"request": request}
    return templates.TemplateResponse(template, context)


async def chatroom_ws(websocket):
    await websocket.accept()
    await run_until_first_complete(
        (chatroom_ws_receiver, {"websocket": websocket}),
        (chatroom_ws_sender, {"websocket": websocket}),
    )


async def chatroom_ws_receiver(websocket):
    async for message in websocket.iter_text():
        await broadcast.publish(channel="chatroom", message=message)


async def chatroom_ws_sender(websocket):
    async with broadcast.subscribe(channel="chatroom") as subscriber:
        async for event in subscriber:
            await websocket.send_text(event.message)


routes = [
    Route("/", homepage),
    WebSocketRoute("/", chatroom_ws, name='chatroom_ws'),
]


app = Starlette(
    routes=routes, on_startup=[broadcast.connect], on_shutdown=[broadcast.disconnect],
)

Installation

  • pip install broadcaster
  • pip install broadcaster[redis]
  • pip install broadcaster[postgres]

## Available backends

  • Broadcast('memory://')
  • Broadcast("redis://localhost:6379")
  • Broadcast("postgres://localhost:5432/hostedapi")

Where next?

  • Serialization / deserialization to support broadcasting structured data.
  • Backends for Redis Streams, 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

broadcaster-0.1.0.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

broadcaster-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file broadcaster-0.1.0.tar.gz.

File metadata

  • Download URL: broadcaster-0.1.0.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for broadcaster-0.1.0.tar.gz
Algorithm Hash digest
SHA256 301d2d573d335a10fc7d7c26245dbd5ef0039c1d7b8064c0480b6a510cc75b4d
MD5 2ba4b00bb28aac26bb62a436213f7078
BLAKE2b-256 d249381ad0f6df89d4ce49d861917b1fef3ca0141213ae5059997dca4d788a61

See more details on using hashes here.

File details

Details for the file broadcaster-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: broadcaster-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/45.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.7.6

File hashes

Hashes for broadcaster-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 824fdbae50b1bf86ce3500c97ef6986060614a193013b85c0684309a0aa119eb
MD5 ea119d8f3032897d5d6c2ffdb03c268b
BLAKE2b-256 d55fa6a523059241a76144edbc370685e3783bd6e289dfd677813dab144244d2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page