Skip to main content
zerotal

References

Configuration

KeyValuesDefault
drivernull, ws, redis, pushernull
pathWebSocket endpoint path/app/ws
redis{ url } — required by the redis driver
pusherPusher credentials — required by the pusher driver

The null driver discards everything broadcast to it. That is the right default for tests and for a development machine with no broker running, and it is why a missing configuration shows up as silence rather than as an error.

The BroadcastEvent interface

Any object implementing this can be handed to Broadcast.send():

MemberRequiredDefaults to
broadcastOn()Yes
broadcastAs()NoThe class's constructor name
broadcastWith()No{}

Because broadcastAs() falls back to the constructor name, renaming an event class silently renames the event clients listen for. Implement it explicitly on anything a browser subscribes to and the wire name stops depending on a refactor.

Commands

@zerotal/broadcasting ships two channel commands:

CommandWhat it does
bun zt channel:listList registered broadcast channel authorization rules
bun zt make:channel OrderChannelAdd a channel authorization rule to routes/channels.ts

Broadcast facade

MethodSignatureDescription
Broadcast.send(event, opts?)(event: BroadcastEvent, opts?: { exceptSocketId?: string }) => voidBroadcast an event to every channel from its broadcastOn().
Broadcast.to(channel, name, data?, opts?)(channel: string, eventName: string, data?: unknown, opts?) => voidPush a raw event to one channel without an event class.
Broadcast.on(channel)(channel: string) => AnonymousBroadcastBegin an anonymous public broadcast (fluent .as().with().send()).
Broadcast.private(channel)(channel: string) => AnonymousBroadcastAnonymous broadcast on a private channel.
Broadcast.presence(channel)(channel: string) => AnonymousBroadcastAnonymous broadcast on a presence channel.
Broadcast.channel(pattern, callback)(pattern: string, callback: ChannelCallback) => voidRegister a channel authorization rule (call in routes/channels.ts).
Broadcast.channels()() => { pattern: string; paramNames: string[] }[]List registered channel patterns.
Broadcast.getMembers(channel)(channel: string) => PresenceMember[]Members of a presence channel (real driver only).
Broadcast.fake()() => BroadcastFakeSwap in an in-memory recorder for tests.
Broadcast.resetFake()() => voidRestore container-backed resolution.

getMembers() reads state the driver holds, so it returns an empty list under the null driver and under fake(). Assert presence membership against a real driver, or assert on the broadcasts themselves instead.

Errors

ErrorThrown when
BroadcastProviderNotRegisteredErrorThe Broadcast facade is used before BroadcastProvider is registered.
MissingChannelParameterErrorA [param] placeholder is interpolated without its value.

Both extend BroadcastError, which extends ZerotalError, so one catch on BroadcastError covers the pair and a ZerotalError handler catches them alongside the rest of the framework's errors.

Broadcast notifications

Notifications can be delivered over a broadcast channel in real time — add 'broadcast' to a notification's channels() and implement toBroadcast(). See Notifications → Broadcasting.

Next steps

  • Broadcasting overview — the guide's front page and the rest of the sections.
  • Events — writing the events this facade sends.
  • Testing — the fake and its assertions.