Arifa
Arifa is a lightweight, Redis-based realtime pub/sub engine for Rust applications.
It provides a simple abstraction over Redis Pub/Sub and WebSocket sessions, making it easy to build scalable realtime systems that work across multiple application nodes.
Features
- Redis Pub/Sub
- Tokio async runtime
- WebSocket session abstraction
- Channel-based subscriptions
- Multi-node message routing
- Online user tracking
- Framework agnostic (
WsSessiontrait)
Installation
Creating an Arifa Instance
use *;
async
Defining a WebSocket Session
Arifa is transport agnostic. Implement the WsSession trait for your WebSocket framework.
use *;
use async_trait;
;
Subscribing
subscribe() returns a tuple containing the spawned task handle and the generated session id.
let session = MySession;
let = arifa.subscribe;
The session id can be used to remove the connection from the online users set when the client disconnects.
Publishing Messages
use *;
use json;
let message = WsMessage ;
arifa
.publish
.await?;
Unsubscribing
unsubscribe() aborts the subscription's Tokio task via JoinHandle::abort(). This is a hard stop, so any code that would otherwise run after the subscription loop — including online-user cleanup — is skipped. Call remove_online_user yourself when the WebSocket closes:
let _ = arifa.remove_online_user.await;
arifa.unsubscribe;
Message Model
NotificationScope
NotificationKind
Online Users
A connection is marked online automatically inside subscribe().
Query the current online count:
let users = arifa.online_users.await?;
println!;
Remove a user when their connection closes:
let _ = arifa.remove_online_user.await;
Note: this count reflects active subscriptions, not unique users — a client with multiple concurrent subscriptions (e.g. multiple open tabs) is counted once per subscription.
Multi-node Routing
To target only a specific application node, set the node_id field.
let message = WsMessage ;
arifa.publish.await?;
If node_id is None, every subscribed node receives the message.
Example: Actix Web
pub async
Architecture
WebSocket Client
│
▼
WsSession
│
▼
Arifa
│
▼
Redis Pub/Sub
│
▼
Other Nodes
Each subscription owns its own Redis Pub/Sub connection and runs inside a dedicated Tokio task.
Use Cases
- Chat applications
- Notifications
- Live location updates
- Multiplayer games
- Live dashboards
- Social feeds
- Collaborative applications
Notes
unsubscribe()usesJoinHandle::abort()(hard stop) — callers are responsible for callingremove_online_userthemselves.- Each subscription spawns a dedicated Tokio task and generates its own session id.
- A Redis Pub/Sub connection is created per subscription.
License
Apache-2.0