Live Update Hooks
Execution Order
For every mutation event, the live-update pipeline runs in this strict order — and short-circuits as soon as any step suppresses the event:
livesetting check — iflive = false, the event is dropped immediately. Ifliveis a function reference, it is called as a gate: returningfalse/nildrops the event and preventsbefore_broadcastfrom running at all.before_broadcasthooks — collection-level first, then global registered hooks. Any hook returningfalse/nilsuppresses the event and stops the chain.- EventBus dispatch — the event is delivered to each matching subscriber, with per-subscriber access checks,
after_readhooks (full mode only), and field stripping (full mode only).
The live filter function and before_broadcast hooks both receive a context table with { collection, operation, data } and have similar shapes, but they sit at different stages: live is the cheap gate, before_broadcast is the transformation/suppression stage.
before_broadcast
A lifecycle event that fires after the write transaction has committed, before the event reaches the EventBus. Hooks can suppress events or transform the broadcast data.
Collection-Level
crap.collections.define("posts", {
hooks = {
before_broadcast = { "hooks.posts.filter_broadcast" },
},
})
The hook function receives { collection, operation, data } and returns:
- The context table (possibly with modified
data) to continue broadcasting falseornilto suppress the event entirely
-- hooks/posts.lua
local M = {}
function M.filter_broadcast(ctx)
if ctx.operation == "delete" then return ctx end
if ctx.data.status == "published" then
return ctx -- broadcast
end
return false -- suppress draft changes
end
return M
Registered Hooks
Global registered hooks also fire for before_broadcast:
-- init.lua
crap.hooks.register("before_broadcast", function(ctx)
-- Strip sensitive fields from all broadcast data
ctx.data._password_hash = nil
ctx.data._reset_token = nil
return ctx
end)
Hook Order Within before_broadcast
- Collection-level
before_broadcasthooks (string refs from definition) - Global registered
before_broadcasthooks (crap.hooks.register)
If any hook returns false/nil, the event is suppressed and no further hooks run.
CRUD Access
before_broadcast hooks run after the transaction has committed and do not have CRUD access.
Data Modifications Are Event-Only
Mutating ctx.data inside a before_broadcast hook affects only the broadcast event payload. The stored document is not updated — the hook fires post-commit, after the write transaction has already closed. Use this to redact fields, decorate the broadcast with computed values, or flatten internal structure for subscribers, but reach for before_change / after_change hooks if you actually need the change to land in the database.
live Setting Functions
When live is a string (Lua function reference), the function is called before before_broadcast hooks:
crap.collections.define("posts", {
live = "hooks.posts.should_broadcast",
})
function M.should_broadcast(ctx)
-- Only broadcast published posts
return ctx.data.status == "published"
end
The function receives { collection, operation, data } and returns true/false. This is a fast gate — before_broadcast hooks only run if the live check passes. See Execution Order above.