Client

Blocking client

The same client without async, behind the blocking feature.
Cargo.toml
[dependencies]
socketio-next = { version = "0.1", features = ["blocking"] }

socketio_next::blocking is the async client driven by a private tokio runtime (one worker thread), with blocking methods and plain-closure handlers. It is one implementation: behaviour, options, states and errors are those of the async client.

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

fn main() -> socketio_next::Result<()> {
    let client = ClientBuilder::new("http://localhost:3000")
        .on("news", |event: Event, client: Client| {
            println!("news: {:?}", event.payload());
            client.emit("thanks", ()).ok();
        })
        .connect()?;

    client.emit("echo", "from a thread")?;
    let reply = client.emit_with_ack("echo", 7, Duration::from_secs(5))?;
    println!("ack: {:?}", reply.arg::<i64>(0)?);

    client.disconnect()
}

The builder has the options of the async ClientBuilder. The Client has emit, emit_with_ack, on / once / off / off_listener / listener_count, state, wait_for, connected, id, namespace, link_info, reconnect and disconnect, all blocking. client.as_async() returns the underlying async Client.

The blocking methods must not be called from inside an async runtime (tokio panics on a nested block_on). Handlers run on blocking threads, where they are fine.