Room: broadcast() method

Available since @irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room host

The broadcast() method of the Room interface sends a message to every connection in the room, or to every connection but one.

Syntax

broadcast(message)
broadcast(message, options)

Parameters

message

The message to send. It may be one of the following types:

Uint8Array
Sent as a binary message.
Any other value
Sent as JSON.
options Optional

An object with any of these properties:

unreliable
A boolean. If true, the message is sent as a WebTransport datagram, or over WebSocket in order with stale ones dropped. Defaults to false.
except
A connection to leave out, usually the one whose message is being relayed.

Return value

None (undefined).

Exceptions

RangeError
Thrown if the message is larger than 64 KB.

Examples

Relaying a message to everyone else

// chat.ts
import { defineRoom } from "@irtio/room";

export default defineRoom({
  onMessage(room, connection, message) {
    room.broadcast({ from: connection.player, message }, { except: connection });
  },
});

See also

Runtime availability

Hosted roomsirt dev@irtio/testingSelf-hosted room host
broadcast()0.10.10.10.1