Livelocd is a lightweight, Axum-compatible plugin for real-time location tracking via WebSockets and a JSON API. It's designed to be easily integrated into any Rust backend for applications like live geolocation dashboards, game user tracking, or delivery fleet monitoring.
- Real-time Location Updates: Send and receive location data using WebSockets.
- Flexible Subscriptions: Subscribe to location updates for all users or specific users.
- RESTful API: Query for the current locations of all users or individual users.
- Token-Based Authentication: Secure your WebSocket connections with a flexible token validation system.
- Minimal Dependencies: Built with a small footprint, relying on
tokio,axum, andserde_json.
Add livelocd to your Cargo.toml dependencies:
[dependencies]
livelocd = { git = "https://github.com/Burnsedia/livelocd" }
# Or, if published on crates.io:
# livelocd = "0.1.0"To get started, add the livelocd routes to your Axum router.
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 ---
// For demonstration, we'll allow any token that starts with "valid-".
// In a real application, you would implement proper token validation.
let validator = ClosureValidator::new(|token: String| token.starts_with("valid-"));
set_token_validator(Arc::new(validator));
// --- 2. Add the livelocd routes to your application ---
let app = Router::new().merge(livelocd_routes());
// --- 3. Start your Axum 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();
}Livelocd uses a TokenValidator trait to authenticate WebSocket connections. You must set a validator using the set_token_validator function before your application starts.
You can implement the TokenValidator trait to create your own custom authentication logic.
use axum::async_trait;
use livelocd::TokenValidator;
struct MyCustomValidator;
#[async_trait]
impl TokenValidator for MyCustomValidator {
async fn validate(&self, token: &str) -> bool {
// Your custom token validation logic here
// For example, check against a database or an auth service
token == "secret-token"
}
}
// Set the validator at startup
// set_token_validator(Arc::new(MyCustomValidator));For simpler validation logic, you can use the ClosureValidator.
use livelocd::{set_token_validator, ClosureValidator};
use std::sync::Arc;
let validator = ClosureValidator::new(|token: String| {
token.starts_with("user-")
});
set_token_validator(Arc::new(validator));All WebSocket endpoints require a token query parameter for authentication.
-
GET /ws/send-location?token=<your-token>Send location updates to the server. The server expects JSON messages with a
user_idand alocation.Example JSON Payload:
{ "user_id": "user123", "location": { "lat": 33.7489954, "lng": -84.3879824 }, "status": "online" } -
GET /ws/subscribe?token=<your-token>Subscribe to real-time location updates for all users.
-
GET /ws/subscribe/:user_id?token=<your-token>Subscribe to real-time location updates for a specific user.
-
GET /api/usersGet the current known locations for all users.
Example Response:
{ "user123": { "location": { "lat": 33.7489954, "lng": -84.3879824 }, "status": "online", "user_id": "user123" } } -
GET /api/users/:user_idGet the most recent location for a single user.
Livelocd maintains an in-memory store of user locations.
LOCATION_MAP: ARwLock<HashMap<String, Value>>that stores the latest JSON payload for eachuser_id.LOCATION_BROADCAST: Atokio::sync::broadcastchannel that fans out location updates to all subscribers.
You can use a command-line tool like websocat to test the WebSocket endpoints.
1. Send a Location:
websocat "ws://localhost:3000/ws/send-location?token=valid-token"
# After connecting, paste and send the JSON payload:
{ "user_id": "car-1", "location": { "lat": 40.7, "lng": -74.0 } }2. Subscribe to All Locations:
websocat "ws://localhost:3000/ws/subscribe?token=valid-token"
# You will receive location updates as they are sent.This project is licensed under the MIT License.