Room: broadcast() method
Available since
@irtio/room 0.1Hosted roomsirt dev@irtio/testingSelf-hosted room hostThe 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.
optionsOptional-
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 tofalse. 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
Room.send()- State sync, for state every client should have rather than a message
Runtime availability
| Hosted rooms | irt dev | @irtio/testing | Self-hosted room host | |
|---|---|---|---|---|
broadcast() | 0.1 | 0.1 | 0.1 | 0.1 |