Back to Documentation

Rust SDK Examples

Production-ready examples showing how to use the OddSockets Rust SDK with async patterns and error handling

Basic Usage
Available
Simple example showing how to connect, subscribe to a channel, and publish messages using async/await patterns with proper error handling.
use oddsockets::{OddSocketsClient, OddSocketsConfig};
use tokio;

#[tokio::main]
async fn main() -> Result<(), Box> {
    // Create a client
    let config = OddSocketsConfig::new("ak_live_1234567890abcdef");
    let client = OddSocketsClient::new(config).await?;

    // Connect to OddSockets
    client.connect().await?;

    // Get a channel
    let channel = client.channel("my-channel");

    // Subscribe to messages
    let mut message_stream = channel.subscribe(Default::default()).await?;
    
    // Publish a message
    channel.publish("Hello, Rust!", Default::default()).await?;

    // Listen for messages
    while let Some(message) = message_stream.recv().await {
        println!("Received: {:?}", message);
    }

    Ok(())
}