channel-box is a lightweight async package for Starlette and FastAPI
that allows you to send messages to named WebSocket channels (groups)
from any part of your backend code.
It is designed for simple real-time communication patterns without external brokers or heavy abstractions.
- Named WebSocket channels (groups)
- Broadcast messages to all connected clients
- Send messages from any part of the backend
- Optional message history per group
- Automatic cleanup of disconnected and expired connections
- Fully async, compatible with FastAPI & Starlette
- Zero external infrastructure (no Redis, no RabbitMQ)
- Group chats
- Backend notifications
- Alerts for user groups
- Real-time status updates
- Internal dashboards
pip install channel-boximport json
from starlette.endpoints import WebSocketEndpoint
from channel_box import Channel, ChannelBox
class WsChatEndpoint(WebSocketEndpoint):
async def on_connect(self, websocket):
group_name = websocket.query_params.get("group_name")
await websocket.accept()
if not group_name:
return
channel = Channel(
websocket=websocket,
expires=60 * 60,
payload_type="json",
)
await ChannelBox.add_channel_to_group(
channel=channel,
group_name=group_name,
)
async def on_receive(self, websocket, data):
payload = json.loads(data)
message = payload.get("message")
username = payload.get("username")
if not message:
return
group_name = websocket.query_params.get("group_name")
if group_name:
await ChannelBox.group_send(
group_name=group_name,
payload={
"username": username,
"message": message,
},
save_history=True,
)from channel_box import ChannelBox
await ChannelBox.group_send(
group_name="MyChat",
payload={
"username": "System",
"message": "Hello from backend",
},
save_history=True,
)groups = await ChannelBox.get_groups()
print(groups)await ChannelBox.flush_groups()history = await ChannelBox.get_history()
print(history)await ChannelBox.flush_history()await ChannelBox.clean_expired()If you use NGINX as a reverse proxy, make sure WebSocket support is enabled:
http://nginx.org/en/docs/http/websocket.html
pip install uvicorn[standard]https://github.com/Sobolev5/channel-box/tree/master/example
pytesthttps://github.com/Sobolev5/channel-box
MIT