Topics and realtime

Tokens for app realtime (live rows per user, broadcast and presence channels over WebSocket), and topics and row-changed feeds over server-sent events.

4 endpointsHTTP API referenceGuide: Realtime guide

On this page
  1. Authentication
  2. Endpoints
  3. POST /topics/:name/publish
  4. GET /topics/:name/subscribe
  5. POST /realtime/connect
  6. POST /realtime/subscribe

Base URL: your app's origin ($APP_ORIGIN): the dev site (https://dev-<slug>.apps.appbricx.com), the published host or a custom domain. Errors answer { ok: false, error: { code, message } } unless noted. The host decides the environment: the dev site reads and writes the dev database, the published host and custom domains production's.

Authentication

App key + data marker
A project key (dpk_c_… in browsers, bound to the app's origins and sent with Origin; dpk_s_… on servers) plus the data-API marker header. With an app session (the header, or the appbricx_app_session cookie) the call acts as that user under row-level security; without one it acts as a signed-out visitor. A server key may assert x-appbricx-app-user and x-appbricx-app-role. Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)

Endpoints

Publish on a topic

POST/__appbricx/topics/:name/publish

Publish a message to every subscriber of a topic. app:* and free-form names take any authenticated caller; row-changed:* takes server keys only.

Auth
App key + data marker: Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)

Path parameters

NameTypeDescription
:namerequiredstringLetters, digits and _ : . - (e.g. app:kitchen).

Body

NameTypeDescription
payloadoptionalany JSONThe message.

Response

Response
{
  "ok": true
}

Errors

StatusCodeWhen
401UNAUTHORIZEDMissing or invalid Authorization bearer.
400PARAMS_INVALIDThe x-appbricx-data-api: 1 header is missing.
403ORIGIN_NOT_ALLOWEDA browser key used from an origin it isn't bound to (ORIGIN_REQUIRED when there is no Origin at all).
429RATE_LIMITEDThe project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user).
400TOPIC_INVALIDBad name.
403TOPIC_PUBLISH_FORBIDDENrow-changed:* without a server key.
curl
curl -X POST "$APP_ORIGIN/__appbricx/topics/app:kitchen/publish" \
  -H "Authorization: Bearer $APP_KEY" \
  -H "x-appbricx-data-api: 1" \
  -H "x-appbricx-app-session: $APP_SESSION" \
  -H "content-type: application/json" \
  -d '{"payload":{"order":"1042","status":"ready"}}'
fetch
const res = await fetch(`${APP_ORIGIN}/__appbricx/topics/app:kitchen/publish`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${APP_KEY}`,
    "x-appbricx-data-api": "1",
    "x-appbricx-app-session": APP_SESSION,
    "content-type": "application/json",
  },
  body: JSON.stringify({
    "payload": {
      "order": "1042",
      "status": "ready"
    }
  }),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? data.error ?? res.statusText);

Source: internal/svc/runtime/data/topics_http.go:35

Subscribe to a topic

GET/__appbricx/topics/:name/subscribe

A server-sent event stream: event: message with the JSON payload, and a ping every 15 seconds. Browsers' EventSource can't set headers, so this route also takes access_token and app_session as query parameters and doesn't need the data marker.

Auth
App key + data marker: Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)

Path parameters

NameTypeDescription
:namerequiredstringapp:*, a free-form name, or row-changed:<table> (needs read access to the table).

Query parameters

NameTypeDescription
access_tokenoptionalstringThe app key, for EventSource.
app_sessionoptionalstringThe user's session, for EventSource.

Response

text/event-stream.

Response
event: message
data: {"event":"created","table":"tickets"}

event: ping
data:

Errors

StatusCodeWhen
401UNAUTHORIZEDMissing or invalid Authorization bearer.
429RATE_LIMITEDThe project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user).
403TOPIC_FORBIDDENUnknown namespace, or no read access to the table.
curl
curl -N "$APP_ORIGIN/__appbricx/topics/row-changed:tickets/subscribe" \
  -H "Authorization: Bearer $APP_KEY" \
  -H "x-appbricx-data-api: 1" \
  -H "x-appbricx-app-session: $APP_SESSION"
fetch
const res = await fetch(`${APP_ORIGIN}/__appbricx/topics/row-changed:tickets/subscribe`, {
  headers: {
    Authorization: `Bearer ${APP_KEY}`,
    "x-appbricx-data-api": "1",
    "x-appbricx-app-session": APP_SESSION,
  },
});
const reader = res.body!.getReader(); // text/event-stream

Source: internal/svc/runtime/data/topics_http.go:36

Realtime connection token

POST/__appbricx/realtime/connect

A connection token for the app's realtime socket (WebSocket at url, Centrifuge protocol v2, JSON), plus what this caller may follow. Signed-in callers connect as their app user; a signed-out caller gets an anon: id only when the app has a public channel or a live table the guest role may read. The SDK (app.realtime) calls this for you.

Auth
App key + data marker: Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)

Body

NameTypeDescription
anonoptionalstringA previous connection token of this visitor: keeps the same anon: id across a refresh.

Response

The token lives 15 minutes; the client refreshes it. live and channels list only what this caller may use.

Response
{
  "ok": true,
  "url": "/__appbricx/realtime/ws",
  "token": "eyJhbGciOiJIUzI1NiIs…",
  "tenant": "6f1c…-….prod",
  "user": "1b2c…",
  "expires_in": 900,
  "live": [
    "message"
  ],
  "channels": [
    {
      "name": "room",
      "presence": true,
      "keyed": true,
      "publish": true
    }
  ]
}

Errors

StatusCodeWhen
401UNAUTHORIZEDMissing or invalid Authorization bearer.
400PARAMS_INVALIDThe x-appbricx-data-api: 1 header is missing.
403ORIGIN_NOT_ALLOWEDA browser key used from an origin it isn't bound to (ORIGIN_REQUIRED when there is no Origin at all).
429RATE_LIMITEDThe project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user).
401SIGN_IN_REQUIREDSigned out, and nothing in the app is open to guests.
503REALTIME_DISABLEDThe server has no realtime service configured, or the app declares no realtime.
curl
curl -X POST "$APP_ORIGIN/__appbricx/realtime/connect" \
  -H "Authorization: Bearer $APP_KEY" \
  -H "x-appbricx-data-api: 1" \
  -H "x-appbricx-app-session: $APP_SESSION" \
  -H "content-type: application/json" \
  -d '{}'
fetch
const res = await fetch(`${APP_ORIGIN}/__appbricx/realtime/connect`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${APP_KEY}`,
    "x-appbricx-data-api": "1",
    "x-appbricx-app-session": APP_SESSION,
    "content-type": "application/json",
  },
  body: JSON.stringify({}),
});
const data = await res.json();
if (!res.ok) throw new Error(data.error?.message ?? data.error ?? res.statusText);

Source: internal/svc/runtime/rtauth/rtauth.go:83

Realtime subscription token

POST/__appbricx/realtime/subscribe

A token for one channel, checked against app.config.ts realtime and the caller's access every time. live:<table> needs the table listed in realtime.live and read access for the caller's role, and registers the caller's identity for that table's change feed. <name> or <name>:<key> is a channel of realtime.channels: public, signed_in or listed roles; a per-row channel needs read access to row <key>. The token says whether the caller may send (publish) and sees presence.

Auth
App key + data marker: Authorization: Bearer $APP_KEYx-appbricx-data-api: 1x-appbricx-app-session: $APP_SESSION (optional: acts as that user)

Body

NameTypeDescription
channelrequiredstringlive:<table>, <channel> or <channel>:<key>.
connection_tokenrequiredstringThe caller's own token from realtime/connect.

Response

Response
{
  "ok": true,
  "token": "eyJhbGciOiJIUzI1NiIs…",
  "channel": "t:6f1c…-….prod:ch:room:3f2a…",
  "expires_in": 900
}

Errors

StatusCodeWhen
401UNAUTHORIZEDMissing or invalid Authorization bearer.
400PARAMS_INVALIDThe x-appbricx-data-api: 1 header is missing.
403ORIGIN_NOT_ALLOWEDA browser key used from an origin it isn't bound to (ORIGIN_REQUIRED when there is no Origin at all).
429RATE_LIMITEDThe project's per-minute request budget is spent (600 a minute, 1,200 with a server key; a quarter of that per IP and per user).
400PARAMS_INVALIDNo channel, or a per-row channel without its key.
401TOKEN_INVALIDThe connection token is missing, expired, forged, or another user's or app's.
403CHANNEL_FORBIDDENThe caller's role may not read the table or join the channel, or can't read that row.
404CHANNEL_UNKNOWNNot a live table or channel of this app.
503REALTIME_DISABLEDNo realtime on this server or in this app.
curl
curl -N -X POST "$APP_ORIGIN/__appbricx/realtime/subscribe" \
  -H "Authorization: Bearer $APP_KEY" \
  -H "x-appbricx-data-api: 1" \
  -H "x-appbricx-app-session: $APP_SESSION" \
  -H "content-type: application/json" \
  -d '{"channel":"room:3f2a…","connection_token":"eyJhbGciOiJIUzI1NiIs…"}'
fetch
const res = await fetch(`${APP_ORIGIN}/__appbricx/realtime/subscribe`, {
  method: "POST",
  headers: {
    Authorization: `Bearer ${APP_KEY}`,
    "x-appbricx-data-api": "1",
    "x-appbricx-app-session": APP_SESSION,
    "content-type": "application/json",
  },
  body: JSON.stringify({
    "channel": "room:3f2a…",
    "connection_token": "eyJhbGciOiJIUzI1NiIs…"
  }),
});
const reader = res.body!.getReader(); // text/event-stream

Source: internal/svc/runtime/rtauth/rtauth.go:84