Client

Connection state

ConnectionState, DisconnectReason, LinkInfo, the heartbeat, reconnect() and shutdown.

States

pub enum ConnectionState {
    Connecting { attempt },
    Connected { sid, recovered },
    Disconnected { reason, error },
    Reconnecting { attempt, delay, error },
    Closed { reason, error },
}
Read itHow
nowclient.state(), client.connected(), client.id(), client.recovered()
waitclient.wait_for(|s| s.is_connected()).await
watchclient.state_watch() (a tokio::sync::watch::Receiver)
every transition, in order with the eventson_state_change(|state, client| ..)

Connected { recovered: true } means the server restored the session (connection state recovery: the missed packets were replayed, the socket id kept).

Disconnect reasons

DisconnectReason has socket.io-client's reasons plus three of its own; reason.as_str() gives the string.

ReasonStringWhen
IoServerDisconnectio server disconnectthe server called socket.disconnect()
IoClientDisconnectio client disconnectclient.disconnect()
PingTimeoutping timeoutno ping within pingInterval + pingTimeout
TransportClosetransport closethe connection was closed
TransportErrortransport errorthe connection failed
ParseErrorparse errorthe server sent an undecodable packet
ConnectRejectedconnect rejectedthe namespace refused the CONNECT (no retry)
ReconnectFailedreconnect failedreconnection attempts used up
Droppedclient droppedthe last Client was dropped

Disconnected { reason, error } and Closed { reason, error } carry the error that ended the connection, such as WebsocketClosed { code, reason } from a close frame.

client.link_info() returns a LinkInfo snapshot:

FieldWhat
sid, engine_sidsocket id and engine.io session id while connected
transportTransportKind in use
ping_interval, ping_timeout, max_payloadfrom the last handshake
connected_at, last_packet_at, last_ping_at, last_attempt_atInstants
reconnect_attemptssince the last successful connect
pending_acks, buffered_emitswaiting acks, queued offline emits
packets_in, packets_out, bytes_in, bytes_outtraffic counters
sessionssuccessful connects (the first one and every reconnect)

Heartbeat and sleep

The client fails the connection with PingTimeout when no ping arrives within pingInterval + pingTimeout. It also compares the wall clock with the deadline: when the wall clock says the deadline passed but the monotonic clock does not (the machine slept, or NTP stepped the clock forward), the client probes the link (a noop POST over polling, a websocket ping) and gives it pingTimeout to show life; only then is it a PingTimeout. A backward clock step has no effect.

Reconnect after close

client.reconnect().await connects a closed client again (after ConnectRejected, ReconnectFailed, a server disconnect or disconnect()) with the same handlers; auth_with / connect_params_with run again first.

if let ConnectionState::Closed { reason: DisconnectReason::ReconnectFailed, .. } = client.state() {
    client.reconnect().await?;
}

Shutdown

client.disconnect().await leaves the namespace and closes the connection. It is bounded (on a dead link: 1 s to send what is queued and the goodbye, 1 s for the engine.io close) and no handler starts after it returns. Dropping the last Client closes the connection too (Dropped).