permit-broadcaster


Namepermit-broadcaster JSON
Version 0.2.5 PyPI version JSON
download
home_pagehttps://github.com/permitio/broadcaster
SummarySimple broadcast channels (Permit fork)
upload_time2023-08-16 17:17:16
maintainer
docs_urlNone
authorTom Christie
requires_python>=3.8
licenseBSD
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Broadcaster (Permit fork)

This is a fork of [encode/broadcaster](https://github.com/encode/broadcaster).

----

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

It currently supports [Redis PUB/SUB](https://redis.io/topics/pubsub), [Apache Kafka](https://kafka.apache.org/), and [Postgres LISTEN/NOTIFY](https://www.postgresql.org/docs/current/sql-notify.html), plus a simple in-memory backend, that you can use for local development or during testing.

<img src="https://raw.githubusercontent.com/encode/broadcaster/master/docs/demo.gif" alt='WebSockets Demo'>

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

**app.py**

```python
# 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],
)
```

The HTML template for the front end [is available here](https://github.com/encode/broadcaster/blob/master/example/templates/index.html), and is adapted from [Pieter Noordhuis's PUB/SUB demo](https://gist.github.com/pietern/348262).

## Requirements

Python 3.7+

## Installation

* `pip install permit-broadcaster`
* `pip install permit-broadcaster[redis]`
* `pip install permit-broadcaster[postgres]`
* `pip install permit-broadcaster[kafka]`

## Available backends

* `Broadcast('memory://')`
* `Broadcast("redis://localhost:6379")`
* `Broadcast("postgres://localhost:5432/broadcaster")`
* `Broadcast("kafka://localhost:9092")`
* `Broadcast("kafka://broker_1:9092,broker_2:9092")`

## Kafka environment variables

The 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 password
```

For 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 additional `KAFKA_` 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 moment `broadcaster` is in Alpha, and should be considered a working design document.

The API should be considered subject to change. If you *do* want to use Broadcaster in its current
state, make sure to strictly pin your requirements to `broadcaster==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 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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/permitio/broadcaster",
    "name": "permit-broadcaster",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "",
    "author": "Tom Christie",
    "author_email": "tom@tomchristie.com",
    "download_url": "https://files.pythonhosted.org/packages/ac/93/06f0c05dbd891c3eb6102cc149329803dfe4213cf21938baa7367cd36801/permit-broadcaster-0.2.5.tar.gz",
    "platform": null,
    "description": "# Broadcaster (Permit fork)\n\nThis is a fork of [encode/broadcaster](https://github.com/encode/broadcaster).\n\n----\n\nBroadcaster helps you develop realtime streaming functionality by providing\na simple broadcast API onto a number of different backend services.\n\nIt currently supports [Redis PUB/SUB](https://redis.io/topics/pubsub), [Apache Kafka](https://kafka.apache.org/), and [Postgres LISTEN/NOTIFY](https://www.postgresql.org/docs/current/sql-notify.html), plus a simple in-memory backend, that you can use for local development or during testing.\n\n<img src=\"https://raw.githubusercontent.com/encode/broadcaster/master/docs/demo.gif\" alt='WebSockets Demo'>\n\nHere's a complete example of the backend code for a simple websocket chat app:\n\n**app.py**\n\n```python\n# Requires: `starlette`, `uvicorn`, `jinja2`\n# Run with `uvicorn example:app`\nfrom broadcaster import Broadcast\nfrom starlette.applications import Starlette\nfrom starlette.concurrency import run_until_first_complete\nfrom starlette.routing import Route, WebSocketRoute\nfrom starlette.templating import Jinja2Templates\n\n\nbroadcast = Broadcast(\"redis://localhost:6379\")\ntemplates = Jinja2Templates(\"templates\")\n\n\nasync def homepage(request):\n    template = \"index.html\"\n    context = {\"request\": request}\n    return templates.TemplateResponse(template, context)\n\n\nasync def chatroom_ws(websocket):\n    await websocket.accept()\n    await run_until_first_complete(\n        (chatroom_ws_receiver, {\"websocket\": websocket}),\n        (chatroom_ws_sender, {\"websocket\": websocket}),\n    )\n\n\nasync def chatroom_ws_receiver(websocket):\n    async for message in websocket.iter_text():\n        await broadcast.publish(channel=\"chatroom\", message=message)\n\n\nasync def chatroom_ws_sender(websocket):\n    async with broadcast.subscribe(channel=\"chatroom\") as subscriber:\n        async for event in subscriber:\n            await websocket.send_text(event.message)\n\n\nroutes = [\n    Route(\"/\", homepage),\n    WebSocketRoute(\"/\", chatroom_ws, name='chatroom_ws'),\n]\n\n\napp = Starlette(\n    routes=routes, on_startup=[broadcast.connect], on_shutdown=[broadcast.disconnect],\n)\n```\n\nThe HTML template for the front end [is available here](https://github.com/encode/broadcaster/blob/master/example/templates/index.html), and is adapted from [Pieter Noordhuis's PUB/SUB demo](https://gist.github.com/pietern/348262).\n\n## Requirements\n\nPython 3.7+\n\n## Installation\n\n* `pip install permit-broadcaster`\n* `pip install permit-broadcaster[redis]`\n* `pip install permit-broadcaster[postgres]`\n* `pip install permit-broadcaster[kafka]`\n\n## Available backends\n\n* `Broadcast('memory://')`\n* `Broadcast(\"redis://localhost:6379\")`\n* `Broadcast(\"postgres://localhost:5432/broadcaster\")`\n* `Broadcast(\"kafka://localhost:9092\")`\n* `Broadcast(\"kafka://broker_1:9092,broker_2:9092\")`\n\n## Kafka environment variables\n\nThe following environment variables are exposed to allow SASL authentication with Kafka (along with their default assignment):\n\n```\nKAFKA_SECURITY_PROTOCOL=PLAINTEXT   # PLAINTEXT, SASL_PLAINTEXT, SASL_SSL\nKAFKA_SASL_MECHANISM=PLAIN   # PLAIN, SCRAM-SHA-256, SCRAM-SHA-512\nKAFKA_PLAIN_USERNAME=None   # any str\nKAFKA_PLAIN_PASSWORD=None   # any str\nKAFKA_SSL_CAFILE=None   # CA Certificate file path for kafka connection\nKAFKA_SSL_CAPATH=None   # Path to directory of trusted PEM certificates for kafka connection\nKAFKA_SSL_CERTFILE=None   # Public Certificate path matching key to use for Kafka connection in PEM format\nKAFKA_SSL_KEYFILE=None   # Private key path to use for Kafka connection in PEM format\nKAFKA_SSL_KEY_PASSWORD=None   # Private key password\n```\n\nFor 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 additional `KAFKA_` prefix.\nFor SSL properties see (AIOKafka SSL Context)[https://aiokafka.readthedocs.io/en/stable/api.html#aiokafka.helpers.create_ssl_context].\n\n## Where next?\n\nAt the moment `broadcaster` is in Alpha, and should be considered a working design document.\n\nThe API should be considered subject to change. If you *do* want to use Broadcaster in its current\nstate, make sure to strictly pin your requirements to `broadcaster==0.2.0`.\n\nTo 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...\n\n* Serialization / deserialization to support broadcasting structured data.\n* Backends for Redis Streams, Apache Kafka, and RabbitMQ.\n* 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.\n* Support for pattern subscribes in backends that support it.\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Simple broadcast channels (Permit fork)",
    "version": "0.2.5",
    "project_urls": {
        "Homepage": "https://github.com/permitio/broadcaster"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "abb6c5c7a8128ea0f1b986e7df6b8f6e8d4d14350a590e2eccb1236f4330ed22",
                "md5": "d093f47cd18d86f753e677274916e7a2",
                "sha256": "4ef95320d94986ea6ce2440d7e4327a8ff4cd8377d11ad8042ead00ffb4fb7c3"
            },
            "downloads": -1,
            "filename": "permit_broadcaster-0.2.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d093f47cd18d86f753e677274916e7a2",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 9745,
            "upload_time": "2023-08-16T17:17:14",
            "upload_time_iso_8601": "2023-08-16T17:17:14.682150Z",
            "url": "https://files.pythonhosted.org/packages/ab/b6/c5c7a8128ea0f1b986e7df6b8f6e8d4d14350a590e2eccb1236f4330ed22/permit_broadcaster-0.2.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ac9306f0c05dbd891c3eb6102cc149329803dfe4213cf21938baa7367cd36801",
                "md5": "dddbe068bef07788d45b31bfc9099cbe",
                "sha256": "0208cca537119e5dfb693cceed1538182a77bc3ae66f77be7c23fdfb503d7f6c"
            },
            "downloads": -1,
            "filename": "permit-broadcaster-0.2.5.tar.gz",
            "has_sig": false,
            "md5_digest": "dddbe068bef07788d45b31bfc9099cbe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 10528,
            "upload_time": "2023-08-16T17:17:16",
            "upload_time_iso_8601": "2023-08-16T17:17:16.361681Z",
            "url": "https://files.pythonhosted.org/packages/ac/93/06f0c05dbd891c3eb6102cc149329803dfe4213cf21938baa7367cd36801/permit-broadcaster-0.2.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-16 17:17:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "permitio",
    "github_project": "broadcaster",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "permit-broadcaster"
}
        
Elapsed time: 0.10143s