Livelocd
Livelocd is a lightweight Axum-compatible plugin for real-time location tracking via WebSockets and a JSON API. Easily drop it into any Rust backend to enable live geolocation dashboards, game user tracking, or delivery fleet monitoring.
β¨ Features
- π‘ WebSocket support for sending real-time location updates
- π Subscribe to all users or individual users' locations
- π REST API to query current locations
- π Token-based authentication for WebSocket connections
- π§© Designed as a plugin for Axum apps
- β‘ Built with minimal dependencies, powered by
tokio,axum, andserde_json
π For detailed documentation, please see docs.md.
π¦ Installation
In your projectβs Cargo.toml:
[dependencies] livelocd = { git = "https://github.com/Burnsedia/livelocd" }
π Usage
In your Axum project:
use axum::Router; use livelocd::{livelocd_routes, set_token_validator, ClosureValidator}; use std::sync::Arc; #[tokio::main] async fn main() { // 1. Set up your token validator let validator = ClosureValidator::new(|token: String| token.starts_with("valid-")); set_token_validator(Arc::new(validator)); // 2. Add the livelocd routes let app = Router::new().merge(livelocd_routes()); // 3. Start the server let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); println!("Listening on: {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); }
π‘ WebSocket Endpoints
All WebSocket endpoints require a token query parameter.
GET /ws/send-location?token=<your-token>β Send JSON with auser_idand any arbitrary fields (e.g., lat/lng)GET /ws/subscribe?token=<your-token>β Receive real-time updates for all usersGET /ws/subscribe/:user_id?token=<your-token>β Subscribe to updates for a specific user
Example JSON payload:
{
"user_id": "user123",
"lat": 33.7489954,
"lng": -84.3879824,
"status": "moving"
}π REST API Endpoints
GET /api/usersβ Get current known location for all usersGET /api/users/:user_idβ Get most recent location for a single user
π§ͺ Testing Locally
Use websocat:
# Send location websocat "ws://localhost:3000/ws/send-location?token=valid-token" # Then, send the JSON payload: {"user_id": "car-1", "lat": 40.7, "lng": -74.0} # Subscribe to all websocat "ws://localhost:3000/ws/subscribe?token=valid-token"
π Built With
π License
MIT
π€ Contributing
Pull requests welcome! Letβs make real-time dashboards in Rust even easier.