@plinthjs/broadcasting
Plinth's event broadcasting layer. Public, private and presence channels, the ShouldBroadcast event contract, swappable broadcaster drivers (log, null, and a recording fake with assertions), a channel registry that authorizes {param} channel patterns, and a manager that resolves named drivers. Depends only on @plinthjs/support.
Install
npm install @plinthjs/broadcastingUsage
import {
BroadcastManager,
broadcast,
privateChannel,
type ShouldBroadcast,
} from '@plinthjs/broadcasting'
class OrderShipped implements ShouldBroadcast {
constructor(private readonly order: { id: number; userId: number }) {}
broadcastOn() {
return privateChannel(`orders.${this.order.userId}`)
}
broadcastAs() {
return 'order.shipped' // defaults to the class name when omitted
}
broadcastWith() {
return { id: this.order.id } // defaults to {} when omitted
}
}
const manager = new BroadcastManager({ default: 'log', logSink: (line) => console.log(line) })
await broadcast(new OrderShipped({ id: 7, userId: 1 }), manager.driver())
// Broadcasting [order.shipped] on channels [private-orders.1] with payload {"id":7}broadcast(event, broadcaster?) resolves the event's channels, name and payload and hands them to the broadcaster (a NullBroadcaster when none is given). The helpers it uses are exported too: eventChannels, eventName and eventPayload.
Channels
import {
Channel,
channel,
presenceChannel,
privateChannel,
toChannelArray,
} from '@plinthjs/broadcasting'
new Channel('news').name() // 'news'
privateChannel('orders.1').name() // 'private-orders.1'
presenceChannel('chat.5').name() // 'presence-chat.5'
channel('orders.1', 'private').base() // 'orders.1'
toChannelArray(privateChannel('a')) // [PrivateChannel]Mark an event to broadcast immediately (rather than through a queue) with broadcastNow(event), and detect it with isShouldBroadcastNow(event). isShouldBroadcast(value) checks for a broadcastOn method.
Channel authorization
import { ChannelRegistry } from '@plinthjs/broadcasting'
const channels = new ChannelRegistry()
.channel('orders.{userId}', (user, { userId }) => (user as { id: number }).id === Number(userId))
.channel('chat.{roomId}', (user) => ({ id: (user as { id: number }).id, name: 'Ada' }))
channels.authorize('private-orders.1', { id: 1 }) // true
channels.authorize('private-orders.2', { id: 1 }) // null (denied)
channels.authorize('presence-chat.5', { id: 1 }) // { id: 1, name: 'Ada' } (presence member info)
channels.has('private-orders.9') // trueThe private- / presence- prefix is stripped before matching. A callback returning false, null or undefined denies; an object is returned as-is for presence channels.
Drivers
BroadcastManager registers null (the default), array (a BroadcastFake) and log (requires a logSink option). Add your own with extend(name, factory) or the drivers option; driver() and its alias connection() cache each resolved instance.
Testing
import { BroadcastFake, broadcast } from '@plinthjs/broadcasting'
const fake = new BroadcastFake() // also exported as ArrayBroadcaster
await broadcast(new OrderShipped({ id: 7, userId: 1 }), fake)
fake
.assertBroadcasted('order.shipped')
.assertBroadcastedOn('private-orders.1', (b) => b.payload['id'] === 7)
.assertNotBroadcasted('order.cancelled')
fake.broadcasts() // [{ channels: ['private-orders.1'], event: 'order.shipped', payload: { id: 7 } }]