WireStats

Available since @irtio/client 5.0BrowsersNode

The WireStats interface of the Client API counts a client’s traffic since it was created, across reconnects: messages and bytes in and out, by message type, and the size of the last state update. A client’s is room.stats.

Instance properties

WireStats.messagesIn
The number of protocol messages received.
WireStats.bytesIn
The bytes of the protocol messages received.
WireStats.messagesOut
The number of protocol messages sent.
WireStats.bytesOut
The bytes of the protocol messages sent.
WireStats.received
Messages and bytes received, by server message type. Read only.
WireStats.sent
Messages and bytes sent, by client message type. Read only.
WireStats.lastUpdate
The last whole state update applied: its tick, the messages it was split into and their size.

Description

Sizes are of protocol messages as encoded, without the transport’s own framing, such as a WebSocket frame header. The client updates the counters as messages pass; your code only reads them. To get rates, sample them at an interval and take the difference.

Examples

Showing bandwidth in a debug overlay

A debug overlay samples the counters once a second and shows the traffic in each direction.

// bandwidth.ts
import type { ReadyRoomClient } from "@irtio/client";

export function showBandwidth(room: ReadyRoomClient, show: (line: string) => void) {
  let last = { in: room.stats.bytesIn, out: room.stats.bytesOut };
  return setInterval(() => {
    const now = { in: room.stats.bytesIn, out: room.stats.bytesOut };
    show(`down ${((now.in - last.in) / 1024).toFixed(1)} KB/s, up ${((now.out - last.out) / 1024).toFixed(1)} KB/s`);
    last = now;
  }, 1000);
}

See also

Runtime availability

BrowsersNode
WireStats5.05.0