| Crates.io | axum-realtime-kit |
| lib.rs | axum-realtime-kit |
| version | 0.1.0 |
| created_at | 2025-06-27 00:39:16.918752+00 |
| updated_at | 2025-06-27 00:39:16.918752+00 |
| description | A toolkit for building scalable, real-time applications with Axum, WebSockets, and Redis Pub/Sub. |
| homepage | |
| repository | https://github.com/thou-sif/axum-realtime-kit |
| max_upload_size | |
| id | 1728003 |
| size | 119,651 |
A toolkit for building scalable, real-time applications with Axum, WebSockets, and Redis Pub/Sub. This crate provides the architectural building blocks to handle connection management, state synchronization, and authentication, letting you focus on your application's business logic.
This crate is a great fit if you are building:
The kit is designed around a few key abstractions:
WebsocketService: The central engine. It manages all active connections on a server instance, communicates with Redis, and drives the entire lifecycle. You create one instance of this and share it across your Axum state.MessageHandler Trait: This is where your application's logic lives. You implement this trait to define your message and event types, handle incoming messages, and react to connections/disconnections.upgrade_handler Function: A generic Axum handler that orchestrates authentication and topic validation before upgrading an HTTP request to a WebSocket connection.The core pattern for using this crate involves four main steps.
Add axum-realtime-kit to your Cargo.toml.
[dependencies]
axum-realtime-kit = { version = "0.1.0", features = ["auth"] }
# ... other dependencies like tokio, serde, uuid ...
Define the structs and enums that model your application's data flow, such as your User identity, the ClientMessages you expect, and the ServerEvents you will broadcast.
// Your authenticated user identity
struct User { /* ... */ }
// Messages clients send to the server
enum ClientMessage { /* ... */ }
// Events the server broadcasts to clients
enum ServerEvent { /* ... */ }
Plug your logic into the WebsocketService by implementing two traits on your application's state and a handler struct.
TokenValidator: Implement this on your shared Axum state to define how a string token from a request is validated and turned into your User struct.MessageHandler: Implement this on a dedicated handler struct. This is where you'll write the logic for what happens when a client sends a message.// In your state implementation
#[async_trait]
impl TokenValidator for AppState { /* ... auth logic ... */ }
// In your handler implementation
#[async_trait]
impl MessageHandler for ChatMessageHandler { /* ... message handling logic ... */ }
In your main function, create the WebsocketService, wrap it in your Axum state, and create a route that uses the upgrade_handler to bring it all together.
📝 For a complete, runnable implementation of these steps, please see the
basic_chatexample in the/examplesdirectory.
You can run the included basic_chat example to see the kit in action.
Clone the repository:
git clone https://github.com/your-username/axum-realtime-kit.git
cd axum-realtime-kit
Set up Redis:
Make sure you have a Redis server running. Create a .env file in the project root and add your Redis connection URL:
# .env
REDIS_URL="redis://127.0.0.1/"
Run the server:
cargo run --example basic_chat
The server will start listening on 127.0.0.1:3000.
Connect with WebSocket clients:
Open two separate terminals and use a tool like websocat.
Terminal 1 (Alice): Connect to the general room.
websocat "ws://127.0.0.1:3000/ws/general?token=a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8:alice"
Terminal 2 (Bob): Connect to the same room and send a message.
# Connect
websocat "ws://127.0.0.1:3000/ws/general?token=b1b2b3b4-a1a2-b1b2-c1c2-c3c4c5c6c7c8:bob"
# Type this and press Enter to send a message
> {"SendMessage":{"room":"general","text":"Hello from Bob!"}}
You will see Bob's message broadcast and appear in Alice's terminal.
auth (default): Enables the WsAuth extractor and TokenValidator trait for authenticating WebSocket upgrade requests.coalescing: Enables the CoalescingService, a utility for preventing duplicate concurrent executions of an expensive asynchronous operation.This project is licensed under either of
at your option.