Package: permit-broadcaster
Broadcaster (Permit fork)This is a fork ofencode/broadcaster.Broadcaster helps you develop realtime streaming functionality by providing
a simple broadcast API onto a number of different backend services.It currently supportsRedis PUB/SUB,Apache Kafka, andPostgres 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`frombroadcasterimportBroadcastfromstarlette.applicationsimportStarlettefromstarlette.concurrencyimportrun_until_first_completefromstarlette.routingimportRoute,WebSocketRoutefromstarlette.templatingimportJinja2Templatesbroadcast=Broadcast("redis://localhost:6379")templates=Jinja2Templates("templates")asyncdefhomepage(request):template="index.html"context={"request":request}returntemplates.TemplateResponse(template,context)asyncdefchatroom_ws(websocket):awaitwebsocket.accept()awaitrun_until_first_complete((chatroom_ws_receiver,{"websocket":websocket}),(chatroom_ws_sender,{"websocket":websocket}),)asyncdefchatroom_ws_receiver(websocket):asyncformessageinwebsocket.iter_text():awaitbroadcast.publish(channel="chatroom",message=message)asyncdefchatroom_ws_sender(websocket):asyncwithbroadcast.subscribe(channel="chatroom")assubscriber:asyncforeventinsubscriber:awaitwebsocket.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],)The HTML template for the front endis available here, and is adapted fromPieter Noordhuis's PUB/SUB demo.RequirementsPython 3.7+Installationpip install permit-broadcasterpip install permit-broadcaster[redis]pip install permit-broadcaster[postgres]pip install permit-broadcaster[kafka]Available backendsBroadcast('memory://')Broadcast("redis://localhost:6379")Broadcast("postgres://localhost:5432/broadcaster")Broadcast("kafka://localhost:9092")Broadcast("kafka://broker_1:9092,broker_2:9092")Kafka environment variablesThe following environment variables are exposed to allow SASL authentication with Kafka (along with their default assignment):KAFKA_SECURITY_PROTOCOL=PLAINTEXT   # PLAINTEXT, SASL_PLAINTEXT, SASL_SSL
KAFKA_SASL_MECHANISM=PLAIN   # PLAIN, SCRAM-SHA-256, SCRAM-SHA-512
KAFKA_PLAIN_USERNAME=None   # any str
KAFKA_PLAIN_PASSWORD=None   # any str
KAFKA_SSL_CAFILE=None   # CA Certificate file path for kafka connection
KAFKA_SSL_CAPATH=None   # Path to directory of trusted PEM certificates for kafka connection
KAFKA_SSL_CERTFILE=None   # Public Certificate path matching key to use for Kafka connection in PEM format
KAFKA_SSL_KEYFILE=None   # Private key path to use for Kafka connection in PEM format
KAFKA_SSL_KEY_PASSWORD=None   # Private key passwordFor full details refer to the (AIOKafka options)[https://aiokafka.readthedocs.io/en/stable/api.html#producer-class] where the variable name matches the capitalised env var with an additionalKAFKA_prefix.
For SSL properties see (AIOKafka SSL Context)[https://aiokafka.readthedocs.io/en/stable/api.html#aiokafka.helpers.create_ssl_context].Where next?At the momentbroadcasteris in Alpha, and should be considered a working design document.The API should be considered subject to change. If youdowant to use Broadcaster in its current
state, make sure to strictly pin your requirements tobroadcaster==0.2.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.Add support forsubscribe('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. Wemightalso 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.