Documentation Index
Fetch the complete documentation index at: https://mintlify.com/slunghq/slung/llms.txt
Use this file to discover all available pages before exploring further.
Installation
Add the Slung SDK to your Rust project:
[dependencies]
slung = "0.1.0"
Your workflow must compile to a WASM target:
rustup target add wasm32-unknown-unknown
Your First Workflow
Here’s a complete example that monitors temperature sensors and sends alerts when values exceed a threshold:
use slung::prelude::*;
#[main]
fn main() -> Result<()> {
// Subscribe to live stream updates.
let handle = query_live("AVG:temp:[sensor=1]")?;
poll_handle(handle, on_event, 100.0)?;
Ok(())
}
fn on_event(event: Event, alert_threshold: f64) -> Result<()> {
if event.value > alert_threshold {
println!("event timestamp={} value={}", event.timestamp, event.value);
for producer in event.producers {
writeback_ws(producer, "ALERT: threshold exceeded")?;
}
}
Ok(())
}
Breaking Down the Example
Import the Prelude
The prelude includes all essential types and functions:
Event - Event payload structure
query_live() - Register live queries
query_history() - Execute historical queries
poll_handle() - Poll for new events
writeback_ws() - Send data to WebSocket clients
unix_micros() - Get current timestamp
write_event() - Ingest new events
Main Entry Point
#[main]
fn main() -> Result<()> {
// Workflow logic here
}
The #[main] macro marks your entry point. It must return std::io::Result<()>.
Register a Live Query
let handle = query_live("AVG:temp:[sensor=1]")?;
Function signature:
pub fn query_live(filter: &str) -> Result<QueryHandle>
This subscribes to the running average of the temp series filtered by sensor=1. The runtime returns a QueryHandle (type alias for u64).
Poll for Events
poll_handle(handle, on_event, 100.0)?;
Function signature:
pub fn poll_handle<F, A>(handle: QueryHandle, callback: F, args: A) -> Result<()>
where
F: Fn(Event, A) -> Result<()>,
A: Clone
This polls the handle forever, calling on_event for each event. The third parameter (100.0) is passed to the callback as alert_threshold.
Process Events
fn on_event(event: Event, alert_threshold: f64) -> Result<()> {
if event.value > alert_threshold {
println!("event timestamp={} value={}", event.timestamp, event.value);
for producer in event.producers {
writeback_ws(producer, "ALERT: threshold exceeded")?;
}
}
Ok(())
}
The callback receives:
event: Event - Contains timestamp, value, tags, and producers
alert_threshold: f64 - The argument passed to poll_handle()
When the value exceeds the threshold, it writes back an alert to all connected producers.
Building Your Workflow
Compile to WebAssembly:
cargo build --target wasm32-unknown-unknown --release
The compiled workflow will be at:
target/wasm32-unknown-unknown/release/your_workflow.wasm
Running Your Workflow
Load the workflow into Slung:
slung workflow load your_workflow.wasm
The runtime will instantiate your WASM module and call your main() function.
Error Handling
All SDK functions return std::io::Result<T>. Use ? to propagate errors:
let handle = query_live("AVG:temp:[sensor=1]")?;
writeback_ws(producer, "message")?;
Note that:
query_live() returns Ok(0) on host registration failure (0 is the sentinel value)
query_history() returns 0.0 for both valid zero results and some failures
writeback_http() returns Ok(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