Skip to main content

Crate socketio_next

Crate socketio_next 

Source
Expand description

A Socket.IO v4 client for Rust, with the behaviour of the reference JavaScript client (socket.io-client 4.8 / engine.io-client 6.6): namespaces, auth, acks in both directions, binary data anywhere in the arguments, reconnection with the same backoff, connection state recovery, and polling / websocket transports over rustls or native-tls.

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

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

    client.emit("hello", json!({ "from": "rust" }))?;
    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
}

Re-exports§

pub use engineio::TlsConfig;
pub use engineio::TransportKind;
pub use protocol::Payload;

Modules§

blockingblocking
The client without async: the same crate::Client driven by a private tokio runtime (one worker thread), with blocking methods and plain-closure handlers.
engineio
An async Engine.IO v4 client: HTTP long polling and websocket transports, the upgrade probe, the heartbeat, and TLS through rustls (default) or native-tls.
protocol
Packet codecs of the Engine.IO v4 and Socket.IO v5 protocols, with the validation rules of the reference implementations (engine.io-parser 5.2, socket.io-parser 4.2).

Structs§

AckSender
Answers one server callback. Clones share the “already sent” flag.
Client
A connected socket.io client (one namespace). Cheap to clone; all clones are one client.
ClientBuilder
Configures a Client. Defaults follow socket.io-client 4.8.
ConnectParams
Query parameters and headers computed for one connect attempt (ClientBuilder::connect_params_with). A key given here replaces the builder’s value of the same key for that attempt.
Event
An event from the server: its name, its arguments and, when the server asked for one, the means to acknowledge it.
LinkInfo
A snapshot of the link for health checks and debugging.
ListenerId
Identifies one registered listener (see Client::off_listener).

Enums§

ConnectionState
The connection lifecycle, published on every transition.
Direction
Which way a packet went, for ClientBuilder::inspect_packets.
DisconnectReason
Why a connection ended. The first six are socket.io-client’s disconnect reasons.
Error
Everything that can go wrong in the socket.io client.
TransportType
Flavor of Engine.IO transport.

Traits§

Handler
Something that can handle A (an Event or a ConnectionState) together with the Client.

Type Aliases§

Result
Result alias of this crate.