{"id":9561,"library":"broadcaster","title":"broadcaster Library","description":"Broadcaster provides a simple, asynchronous publish/subscribe interface for Python 3.8+, allowing for broadcast channels across various backends like Redis, PostgreSQL, and Kafka. The current version is 0.3.1. It's actively maintained by the Encode organization, known for projects like Starlette and Uvicorn, suggesting a steady, incremental release cadence.","status":"active","version":"0.3.1","language":"en","source_language":"en","source_url":"https://github.com/encode/broadcaster","tags":["async","messaging","broadcast","pubsub","asyncio"],"install":[{"cmd":"pip install broadcaster","lang":"bash","label":"Base installation"},{"cmd":"pip install \"broadcaster[redis]\" \"broadcaster[postgres]\" \"broadcaster[kafka]\"","lang":"bash","label":"With all optional backends"}],"dependencies":[{"reason":"Required for Redis backend support via 'broadcaster[redis]'.","package":"redis","optional":true},{"reason":"Required for PostgreSQL backend support via 'broadcaster[postgres]'.","package":"asyncpg","optional":true},{"reason":"Required for Kafka backend support via 'broadcaster[kafka]'.","package":"aiokafka","optional":true}],"imports":[{"symbol":"Broadcaster","correct":"from broadcaster import Broadcaster"}],"quickstart":{"code":"import asyncio\nimport os\nfrom broadcaster import Broadcaster\n\nBROADCAST_URL = os.environ.get(\"BROADCAST_URL\", \"memory://\")\n\nasync def main():\n    # Initialize broadcaster with a URL, defaults to in-memory if env var is not set\n    broadcaster = Broadcaster(BROADCAST_URL)\n    \n    # Establish connection to the backend\n    await broadcaster.connect()\n    \n    async with broadcaster.subscribe(\"chat\") as subscriber:\n        # Publish a message\n        await broadcaster.publish(\"chat\", \"Hello from broadcaster!\")\n        print(f\"Published: Hello from broadcaster!\")\n        \n        # Receive a message\n        message = await subscriber.get()\n        print(f\"Received: {message}\")\n\n    # Disconnect from the backend\n    await broadcaster.disconnect()\n\nif __name__ == \"__main__\":\n    asyncio.run(main())","lang":"python","description":"This quickstart demonstrates setting up an in-memory (default) or environment-variable-configured Broadcaster. It connects, publishes a message to a channel, and then subscribes and retrieves that message. Remember to set the `BROADCAST_URL` environment variable (e.g., `redis://localhost:6379`) to use a different backend, and ensure the corresponding optional dependency is installed."},"warnings":[{"fix":"Install the specific backend dependency using extras, e.g., `pip install \"broadcaster[redis]\"` for Redis, or `pip install \"broadcaster[postgres]\"` for PostgreSQL.","message":"Backend dependencies (e.g., Redis, PostgreSQL, Kafka) are optional and must be explicitly installed for each chosen backend. Installing just `broadcaster` will only provide the in-memory backend.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Add `await broadcaster.connect()` at the start of your Broadcaster operations and `await broadcaster.disconnect()` when done, or use context managers for subscriptions where applicable.","message":"Always ensure to `await broadcaster.connect()` before publishing or subscribing, and `await broadcaster.disconnect()` afterwards. Forgetting these can lead to resource leaks or operations failing due to a lack of an active connection.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure all Broadcaster operations are awaited within an `async` function and the application's event loop is properly managed (e.g., using `asyncio.run()` for top-level execution).","message":"Broadcaster is an asyncio-first library. All interaction methods like `publish()` and `subscribe()` are coroutines and must be `await`ed. Mixing synchronous and asynchronous code without proper handling will lead to `TypeError: object 'coroutine' can't be used in 'await' expression` or other runtime errors.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"Install the correct optional dependency for your chosen backend, e.g., `pip install \"broadcaster[redis]\"` for Redis, `pip install \"broadcaster[postgres]\"` for PostgreSQL, or `pip install \"broadcaster[kafka]\"` for Kafka.","cause":"The specific backend dependency was not installed. The base `broadcaster` package only includes the in-memory backend.","error":"ModuleNotFoundError: No module named 'redis'"},{"fix":"Ensure that `asyncio.run()` is used correctly as the entry point for your async application, or if integrating with a larger framework, verify its event loop management. Avoid explicitly closing loops if `asyncio.run()` is managing it.","cause":"This typically occurs in asyncio when attempting to run async code after the event loop has been shut down, or when mismanaging multiple event loops.","error":"RuntimeError: Event loop is closed"},{"fix":"Verify that the backend service is running, is reachable on the specified host and port, and that the `BROADCAST_URL` string is correctly formatted (e.g., `redis://localhost:6379`). Check firewall rules or network configurations if connecting to a remote service.","cause":"The specified backend service (e.g., Redis server, PostgreSQL database, Kafka broker) is not running, is inaccessible from the application's host, or the `BROADCAST_URL` is incorrect.","error":"broadcaster.exceptions.ConnectionError: Unable to connect to backend at ..."}]}