Skip to content

Deployment channel

Use channels for live event streams where clients subscribe to status changes. This example publishes deployment progress and lets a dashboard read recent events after it reconnects.

Terminal window
import { backend } from "@layeron/core"
import { realtime } from "@layeron/modules"
const app = backend()
const live = realtime({
name: "ops",
historyLimit: 200,
})
app.use(live)
Terminal window
app.post("/deployments/:deploymentId/events", async (request) => {
const pathSegments = new URL(request.url).pathname.split("/")
const deploymentId = pathSegments[2]
const body = await request.json()
const channel = live.channel(`deployment_${deploymentId}`)
return await channel.broadcast({
type: body.type,
data: {
deploymentId: deploymentId,
status: body.status,
step: body.step,
message: body.message,
},
idempotencyKey: body.eventId,
})
})
Terminal window
app.get("/deployments/:deploymentId/events", async (request) => {
const pathSegments = new URL(request.url).pathname.split("/")
const deploymentId = pathSegments[2]
return await live.channel(`deployment_${deploymentId}`).history({
limit: Number(new URL(request.url).searchParams.get("limit") ?? 50),
cursor: new URL(request.url).searchParams.get("cursor") ?? undefined,
})
})
Terminal window
app.post("/tenants/:tenantId/alerts", async (request) => {
const pathSegments = new URL(request.url).pathname.split("/")
const tenantId = pathSegments[2]
const body = await request.json()
return await live.channel(`tenant_${tenantId}:alerts`).publish({
type: "alert.created",
data: {
title: body.title,
severity: body.severity,
runbookUrl: body.runbookUrl,
},
})
})

Use one channel per stream you want clients to subscribe to or replay.