WebSocket Writeback
Send data to WebSocket clients connected to the Slung runtime.Function
destination: u64- Producer connection IDdata: &str- Text data to send
Ok(())- Message sent successfullyErr(_)- Failed to send message
Getting Producer IDs
Producer IDs come from theEvent structure:
Example: Alert System
Example: JSON Response
Broadcasting to Multiple Clients
Theproducers field contains all connection IDs that contributed to the aggregate:
HTTP Writeback
Send HTTP requests to external APIs and receive responses.Function
destination: &str- URL to send request todata: &str- Request bodymethod: WritebackMethod- HTTP method
Ok(Some(Vec<u8>))- Response body as bytesOk(None)- No response body or request failed
Region, or 0 when there’s no response body or the request fails.
HTTP Methods
Example: POST Alert
Example: GET External Data
Example: PUT Update
Example: DELETE Resource
Error Handling
WebSocket Errors
HTTP Errors
Advanced Patterns
Dual Writeback
Send to both WebSocket clients and HTTP endpoints:Conditional Writeback
Rate Limiting
Best Practices
- Handle failures gracefully: Writeback can fail if connections close or HTTP requests timeout
- Don’t block on HTTP: HTTP writeback is synchronous and can slow your workflow
- Batch when possible: Aggregate multiple events before writing back
- Use appropriate protocols: WebSocket for real-time client notifications, HTTP for external integrations
- Include context: Send enough information (timestamp, tags) for receivers to process events
- Log failures: Track which producers or endpoints fail to help debug connectivity issues
Next Steps
Live Queries
Learn about live query subscriptions
Historical Queries
Query aggregated historical data