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
}ClientBuilderconfigures and connects;Clientemits, acks, adds handlers at runtime and reportsConnectionState/LinkInfo.blocking(featureblocking) is the same client without async.engineiois the transport layer,protocolthe packet codecs (no I/O).
Re-exports§
pub use engineio::TlsConfig;pub use engineio::TransportKind;pub use protocol::Payload;
Modules§
- blocking
blocking - The client without async: the same
crate::Clientdriven 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.
- Client
Builder - Configures a
Client. Defaults follow socket.io-client 4.8. - Connect
Params - 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.
- Link
Info - A snapshot of the link for health checks and debugging.
- Listener
Id - Identifies one registered listener (see
Client::off_listener).
Enums§
- Connection
State - The connection lifecycle, published on every transition.
- Direction
- Which way a packet went, for
ClientBuilder::inspect_packets. - Disconnect
Reason - Why a connection ended. The first six are socket.io-client’s
disconnectreasons. - Error
- Everything that can go wrong in the socket.io client.
- Transport
Type - Flavor of Engine.IO transport.
Traits§
- Handler
- Something that can handle
A(anEventor aConnectionState) together with theClient.
Type Aliases§
- Result
- Result alias of this crate.