Broadcast
Send and receive ephemeral messages by event. By default the sender does not receive its own message, and send() does not wait for a server ack.
const channel = realtime.channel("broadcast-test", {
config: {
broadcast: { ack: false, self: false },
},
});
channel.on("broadcast", { event: "some-event" }, (payload) => {
console.log(payload);
});
channel.subscribe(async (status) => {
if (status !== "SUBSCRIBED") return;
await channel.send({
type: "broadcast",
event: "some-event",
payload: { hello: "world" },
});
});
| Option | Description |
|---|---|
ack | When true, send() resolves after the server acknowledges the message |
self | When true, the sender also receives its own message |
replay | Private channels only. Replay earlier Broadcasts from since (epoch ms). Optional limit (max 25) |
const twelveHoursAgo = Date.now() - 12 * 60 * 60 * 1000;
const channel = realtime.channel("main:room", {
config: {
private: true,
broadcast: { replay: { since: twelveHoursAgo, limit: 10 } },
},
});
channel.on("broadcast", { event: "my_event" }, (payload) => {
if (payload?.meta?.replayed) {
console.log("replayed message", payload);
} else {
console.log("new message", payload);
}
});
channel.subscribe();
If the WebSocket is not ready, send() falls back to HTTP. Use httpSend() when you want HTTP explicitly.