Installation
Add the Slung SDK to your Rust project:Cargo.toml
Your First Workflow
Here’s a complete example that monitors temperature sensors and sends alerts when values exceed a threshold:src/main.rs
Breaking Down the Example
Import the Prelude
Event- Event payload structurequery_live()- Register live queriesquery_history()- Execute historical queriespoll_handle()- Poll for new eventswriteback_ws()- Send data to WebSocket clientsunix_micros()- Get current timestampwrite_event()- Ingest new events
Main Entry Point
#[main] macro marks your entry point. It must return std::io::Result<()>.
Register a Live Query
temp series filtered by sensor=1. The runtime returns a QueryHandle (type alias for u64).
Poll for Events
on_event for each event. The third parameter (100.0) is passed to the callback as alert_threshold.
Process Events
event: Event- Containstimestamp,value,tags, andproducersalert_threshold: f64- The argument passed topoll_handle()
Building Your Workflow
Compile to WebAssembly:Running Your Workflow
Load the workflow into Slung:main() function.
Error Handling
All SDK functions returnstd::io::Result<T>. Use ? to propagate errors:
query_live()returnsOk(0)on host registration failure (0 is the sentinel value)query_history()returns0.0for both valid zero results and some failureswriteback_http()returnsOk(None)when there’s no response body or the request fails
Next Steps
Live Queries
Learn about live query subscriptions and polling
Historical Queries
Query aggregated historical data