Documentation / @zerotal/broadcasting / broadcastsModelEvents
Function: broadcastsModelEvents()
broadcastsModelEvents<
M>(ModelClass,options):void
Defined in: broadcasting/src/BroadcastsModelEvents.ts:50
Auto-broadcast a model's lifecycle events.
It populates the model's dispatchesEvents map with generated BroadcastingEvent subclasses,
reusing the existing model-event bridge: when the model is created/updated/deleted, the event
is fired on the application Events bus (so listeners run) and broadcast (so connected clients
receive it). Call it once — at the bottom of the model file or in a provider's boot.
Type Parameters
M
M extends object
Parameters
ModelClass
ModelClassWithEvents<M>
options
BroadcastsModelEventsOptions<M>
Returns
void
Example
// app/models/Order.ts
import { BaseModel, column, table } from "@zerotal/orm";
import { broadcastsModelEvents, privateChannel } from "@zerotal/broadcasting";
@table("orders")
export class Order extends BaseModel {
@column() id!: number;
@column() status!: string;
}
broadcastsModelEvents(Order, {
channels: (order) => privateChannel(`orders.${order.id}`),
// optional: with: (order) => ({ id: order.id, status: order.status }),
});
// Client: Echo.private(`orders.${id}`).listen("OrderUpdated", (e) => ...)