socketio-next 0.2.0

Socket.IO v4 / Engine.IO v4 client with socket.io-client 4.8 parity: async (tokio) and blocking, rustls or native-tls
Documentation

Crates.io Downloads docs.rs Guide CI MSRV License GitHub issues GitHub stars Donate

socketio-next

A Socket.IO v4 client for Rust that behaves like socket.io-client 4.8.

Namespaces, auth, acks in both directions, binary data anywhere in the arguments, the same reconnection backoff, connection state recovery, polling and websocket transports, proxies, TLS through rustls or native-tls. Async (tokio) with an optional blocking API, and a Tauri 2 plugin.

Documentation: s00d.github.io/rust-socketio · API reference: docs.rs/socketio-next

Install

[dependencies]
socketio-next = "0.1"
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }

Cargo features: rustls (default), native-tls, platform-verifier, cookies, blocking (details).

use std::time::Duration;
use serde_json::json;
use socketio_next::{Client, ClientBuilder, Event};

#[tokio::main]
async fn main() -> socketio_next::Result<()> {
    let client = ClientBuilder::new("http://localhost:3000")
        .auth(json!({ "token": "abc" }))
        .on("chat", |event: Event, _client: Client| async move {
            println!("chat: {:?}", event.payload());
        })
        .connect()
        .await?;

    client.emit("chat", "hello")?;
    let reply = client
        .emit_with_ack("add", vec![json!(1), json!(2)], Duration::from_secs(5))
        .await?;
    println!("1 + 2 = {}", reply.arg::<i64>(0)?);

    client.disconnect().await
}

Tauri

tauri-plugin-socketio (npm tauri-plugin-socketio-api) shares named clients between Rust and the webviews of a Tauri 2 app.

# src-tauri/Cargo.toml
tauri-plugin-socketio = "0.1"
pnpm add tauri-plugin-socketio-api

See the plugin guide.

Coming from rust_socketio

socketio-next started as a fork of rust_socketio 0.6 and rewrote its client. See the migration guide.

Support

If socketio-next helps you, you can support it via DonationAlerts. Bugs and ideas: issues.

License

MIT, see LICENSE. Based on rust_socketio by Bastian Kersting and the rust-socketio contributors.