Online User Management & Real-time Push
English
Overview
The Online User Management module provides real-time tracking of user online status and message push capabilities. Perfect for building chat applications, live notifications, and real-time collaboration tools.
Key Features
- Online Status Tracking - Track user connections in real-time
- Multi-Device Support - Users can connect from multiple devices
- Real-time Push - Send messages to specific users or broadcast to all
- Kick-Out Notifications - Force logout with notifications
- Activity Tracking - Monitor user activity timestamps
- Extensible Pushers - Implement custom push mechanisms
Quick Start
rust
use sa_token_core::{OnlineManager, OnlineUser, InMemoryPusher};
use std::sync::Arc;
use std::collections::HashMap;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create online manager
let manager = Arc::new(OnlineManager::new());
// Register message pusher
let pusher = Arc::new(InMemoryPusher::new());
manager.register_pusher(pusher.clone()).await;
// Mark user as online
let user = OnlineUser {
login_id: "user123".to_string(),
token: "token123".to_string(),
device: "web".to_string(),
connect_time: chrono::Utc::now(),
last_activity: chrono::Utc::now(),
metadata: HashMap::new(),
};
manager.mark_online(user).await;
// Push message to user
manager.push_to_user("user123", "Hello!".to_string()).await?;
// Broadcast to all users
manager.broadcast("System announcement".to_string()).await?;
// Check online status
if manager.is_online("user123").await {
println!("User is online");
}
Ok(())
}API Reference
OnlineManager Methods
new()- Create managermark_online(user)- Mark user onlinemark_offline(login_id, token)- Mark specific session offlinemark_offline_all(login_id)- Mark all user sessions offlineis_online(login_id)- Check if user is onlineget_online_count()- Get total online usersget_online_users()- Get list of online user IDspush_to_user(login_id, content)- Push to single userpush_to_users(login_ids, content)- Push to multiple usersbroadcast(content)- Push to all online userskick_out_notify(login_id, reason)- Force logout with notification
Message Types
MessageType::Text- Plain textMessageType::Binary- Binary dataMessageType::KickOut- Logout notificationMessageType::Notification- System notificationMessageType::Custom(String)- Custom type
Related Documentation
License
MIT OR Apache-2.0