Skip to main content

Fast Channels brings Django Channels–style consumers and channel layers to FastAPI and Starlette for real-time apps.

Project description

PyPI Code Coverage Test Checked with mypy Checked with pyright Docstring

Fast Channels brings Django Channels–style consumers and channel layers to FastAPI, Starlette, and any ASGI-compatible framework for real-time apps.

What are Channel Layers?

Channel layers enhance WebSocket functionality by enabling advanced messaging patterns beyond simple request-response:

  • Group Messaging: Send messages to multiple WebSocket connections simultaneously

  • Cross-Instance Communication: Send messages from HTTP endpoints, background workers, or other processes

  • Task Distribution: Use as a basic task queue for offloading work to worker processes

  • Distributed Applications: Build scalable real-time apps without routing everything through a database

As the Django Channels documentation explains: “Channel layers allow you to talk between different instances of an application. They’re a useful part of making a distributed realtime application if you don’t want to have to shuttle all of your messages or events through a database.”

Fast Channels ports this proven architecture from Django Channels, which has been battle-tested in production environments for years. By leveraging Redis for high-performance, reliable message delivery while maintaining the familiar consumer-based programming model, Fast Channels brings Django’s mature WebSocket patterns to the entire ASGI ecosystem.

Features

  • Django Channels-Style Consumers: Familiar WebSocket consumer patterns with connect, receive, and disconnect methods

  • Channel Layers: Support for Redis-backed channel layers with group messaging

  • Redis Integration: Full Redis support with Redis Sentinel for high availability

  • Async/Await Support: Built from the ground up for modern Python async programming

  • ASGI Compatible: Seamless integration with FastAPI, Starlette, and any ASGI-compatible framework

  • Testing Framework: Comprehensive testing utilities for WebSocket consumers

  • Full Type Safety: Complete type hints with mypy and pyright support

  • Multiple Channel Layer Backends: In-memory for development, Redis for production

Installation

# Basic installation
pip install fast-channels

# Recommended: Install with Redis support for production use
pip install fast-channels[redis]

Optional Dependencies:

# For Redis channel layer support (recommended for production)
pip install fast-channels[redis]

Note: Additional channel layer backends (such as Kafka) may be supported in future releases.

Core Dependencies: Python 3.11+, compatible with any ASGI framework (FastAPI, Starlette, Quart, etc.)

Quick Start

  1. Register Channel Layers

# layers.py
import os
from fast_channels.layers import (
    InMemoryChannelLayer,
    RedisChannelLayer,
    RedisPubSubChannelLayer,
    register_channel_layer,
)

def setup_layers():
    # Redis URL from environment
    redis_url = os.getenv("REDIS_URL", "redis://localhost:6379")

    # Register different layer types
    register_channel_layer("memory", InMemoryChannelLayer())
    register_channel_layer("chat", RedisPubSubChannelLayer(hosts=[redis_url], prefix="chat"))
    register_channel_layer("queue", RedisChannelLayer(
        hosts=[redis_url],
        prefix="queue",
        expiry=900,  # 15 minutes
        capacity=1000,
    ))
  1. Create WebSocket Consumer

# consumer.py
from fast_channels.consumer.websocket import AsyncWebsocketConsumer

class ChatConsumer(AsyncWebsocketConsumer):
    groups = ["chat_room"]
    channel_layer_alias = "chat"  # Use registered layer

    async def connect(self):
        await self.accept()
        await self.channel_layer.group_send(
            "chat_room",
            {"type": "chat_message", "message": "Someone joined the chat"}
        )

    async def disconnect(self, close_code):
        await self.channel_layer.group_send(
            "chat_room",
            {"type": "chat_message", "message": "Someone left the chat"}
        )

    async def receive(self, text_data=None, bytes_data=None, **kwargs):
        await self.channel_layer.group_send(
            "chat_room",
            {"type": "chat_message", "message": f"Message: {text_data}"}
        )

    async def chat_message(self, event):
        # Send message to WebSocket
        await self.send(event["message"])
  1. Integrate with FastAPI

# main.py
from fastapi import FastAPI
from .layers import setup_layers
from .consumer import ChatConsumer

# Setup layers before creating the app
setup_layers()

app = FastAPI()

# Create WebSocket sub-app for better organization
ws_app = FastAPI()
ws_app.add_websocket_route("/chat", ChatConsumer.as_asgi())

# Mount WebSocket routes
app.mount("/ws", ws_app)

Channel Layer Backends

In-Memory (Testing Only)
  • Fast and simple for unit tests

  • Single-process only - cannot send messages from workers or HTTP endpoints

  • No persistence

  • Use only for testing group chat functionality or running test suites

Redis Queue Layer (Production - Reliable)
  • Message persistence and guaranteed delivery

  • Scalable across multiple processes

  • Configurable expiry and capacity

  • Best for critical messaging

Redis Pub/Sub Layer (Production - Real-time)
  • Ultra-low latency messaging

  • Real-time broadcasting

  • No message persistence

  • Best for live chat and notifications

Registration Examples:

from fast_channels.layers import (
    InMemoryChannelLayer,
    RedisChannelLayer,
    RedisPubSubChannelLayer,
    register_channel_layer,
)

# In-memory for development
register_channel_layer("memory", InMemoryChannelLayer())

# Redis Queue Layer for reliable messaging
register_channel_layer("reliable", RedisChannelLayer(
    hosts=["redis://localhost:6379"],
    prefix="app_queue",
    capacity=1500,
    expiry=3600,  # 1 hour
))

# Redis Pub/Sub for real-time chat
register_channel_layer("chat", RedisPubSubChannelLayer(
    hosts=["redis://localhost:6379"],
    prefix="app_chat",
))

# Redis Sentinel for high availability
register_channel_layer("ha_queue", RedisChannelLayer(
    sentinels=[("localhost", 26379)],
    service_name="mymaster",
    sentinel_kwargs={"password": "sentinel_password"},
    connection_kwargs={"password": "redis_password"},
))

Testing

Fast Channels includes comprehensive testing utilities out of the box:

from fast_channels.testing import WebsocketCommunicator
import pytest

@pytest.mark.asyncio
async def test_chat_consumer():
    communicator = WebsocketCommunicator(ChatConsumer, "/ws/chat/")
    connected, subprotocol = await communicator.connect()
    assert connected

    # Test sending a message
    await communicator.send_json_to({
        "message": "hello world"
    })
    response = await communicator.receive_json_from()
    assert response == {"message": "hello world"}

    await communicator.disconnect()

Documentation

Please visit Fast Channels docs for complete documentation, including:

  • Detailed consumer patterns

  • Advanced channel layer configuration

  • Production deployment guides

  • Testing best practices

  • Migration guides from Django Channels

Comparison with Alternatives

Fast Channels vs. Native FastAPI/Starlette WebSockets

Native FastAPI WebSocket support provides basic connection handling but lacks advanced messaging capabilities:

# Native FastAPI - Limited to direct connections
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    # Can only send/receive to this specific connection
    # No group messaging or cross-process communication

Fast Channels vs. Broadcaster

Broadcaster is a lightweight pub/sub library, but Fast Channels provides more comprehensive functionality:

Feature

Native WS

Broadcaster

Fast Channels

Basic WebSocket

Simple Pub/Sub

Group Messaging

Consumer Pattern

Message Persistence

✅ (Redis Queue)

Testing Framework

Connection Management

Manual

Manual

Automatic

Type Safety

Manual

Basic

Full

Background Worker Msgs

Structured Event System

Contributing

Contributions are welcome! Please see our CONTRIBUTING.md for detailed development setup and guidelines.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

fast_channels-0.1.1.tar.gz (34.6 kB view details)

Uploaded Source

Built Distribution

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

fast_channels-0.1.1-py3-none-any.whl (45.5 kB view details)

Uploaded Python 3

File details

Details for the file fast_channels-0.1.1.tar.gz.

File metadata

  • Download URL: fast_channels-0.1.1.tar.gz
  • Upload date:
  • Size: 34.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_channels-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2168a4e227e45a7041971dfee01ace5bc189915cce937af4f709dfaf1ffcdbf3
MD5 785123c688a37c650413fa957dbf59ce
BLAKE2b-256 45aee2992528c18e8f811af94255829be0e632563575169c625cb776b0816cfd

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_channels-0.1.1.tar.gz:

Publisher: publish.yml on huynguyengl99/fast-channels

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fast_channels-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: fast_channels-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 45.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for fast_channels-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1d8bed2ef4141b2ae6cca10ab999ffdfcd2acf2614c979c0bb88b5c08f4be69c
MD5 c95677967ce85c66217cbb6f61e1bf19
BLAKE2b-256 c6a6fc0017375aaa283cd008605f6b9de7243cf16576a40ae1a24ea06051591c

See more details on using hashes here.

Provenance

The following attestation bundles were made for fast_channels-0.1.1-py3-none-any.whl:

Publisher: publish.yml on huynguyengl99/fast-channels

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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