Skip to main content

ChannelBox is a package for FastAPI & Starlette that allows you to make named websocket channels.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

channel_box-1.2.0.tar.gz (10.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

channel_box-1.2.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file channel_box-1.2.0.tar.gz.

File metadata

  • Download URL: channel_box-1.2.0.tar.gz
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for channel_box-1.2.0.tar.gz
Algorithm Hash digest
SHA256 beab93eaff23a9239924a49949103577c08b401a662785ae93e6fd98ef207fcf
MD5 c04ed5d4da572c50de8d4d481ffb564c
BLAKE2b-256 c00930c22b6f070a8f7128ee343124115cb7ba06f1d29b84592b9c92e73f97e2

See more details on using hashes here.

File details

Details for the file channel_box-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: channel_box-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for channel_box-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d7543550a8df8a76c630e9f62eef3efd278ae4fe1a794087a6e0318b09d76016
MD5 c810f207a2abe1bb847e4d200b9db5ad
BLAKE2b-256 806fc5580611bb2d019f11bcefb4eba7ad55e143452e87c9372e8a04842e341b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page