Skip to main content

channel-box

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.


Features

  • 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)

Typical use cases

  • Group chats
  • Backend notifications
  • Alerts for user groups
  • Real-time status updates
  • Internal dashboards

Installation

pip install channel-box

Basic usage example

WebSocket endpoint

import 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,
            )

Send messages from anywhere in backend

from channel_box import ChannelBox

await ChannelBox.group_send(
    group_name="MyChat",
    payload={
        "username": "System",
        "message": "Hello from backend",
    },
    save_history=True,
)

Groups management

Get active groups

groups = await ChannelBox.get_groups()
print(groups)

Flush all groups

await ChannelBox.flush_groups()

Message history

Get history

history = await ChannelBox.get_history()
print(history)

Flush history

await ChannelBox.flush_history()

Cleanup expired connections

await ChannelBox.clean_expired()

NGINX WebSocket configuration

If you use NGINX as a reverse proxy, make sure WebSocket support is enabled:

http://nginx.org/en/docs/http/websocket.html

Uvicorn

pip install uvicorn[standard]

Full working example

https://github.com/Sobolev5/channel-box/tree/master/example

Tests

pytest

Repository

https://github.com/Sobolev5/channel-box

License

MIT

Metadata

Release files for channel-box 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for channel-box 1.2.0
File Size Uploaded Provenance Actions
channel_box-1.2.0.tar.gz 10.8 kB Without provenance Details for channel_box-1.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for channel-box 1.2.0
File Interpreter ABI Platform Actions
channel_box-1.2.0-py3-none-any.whl Python 3 none any Details for channel_box-1.2.0-py3-none-any.whl

Total release size: 16.9 kB

Release files / channel_box-1.2.0.tar.gz

Download URL channel_box-1.2.0.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
beab93eaff23a9239924a49949103577c08b401a662785ae93e6fd98ef207fcf
BLAKE2b-256 checksum
How to use checksums
c00930c22b6f070a8f7128ee343124115cb7ba06f1d29b84592b9c92e73f97e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / channel_box-1.2.0-py3-none-any.whl

Download URL channel_box-1.2.0-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d7543550a8df8a76c630e9f62eef3efd278ae4fe1a794087a6e0318b09d76016
BLAKE2b-256 checksum
How to use checksums
806fc5580611bb2d019f11bcefb4eba7ad55e143452e87c9372e8a04842e341b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for channel-box 1.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page