Docs / Build / Realtime

Realtime

Realtime sends live updates over WebSockets. It is available to every project with nothing to enable.

Channels and broadcast

const room = fastbase.channel("room:123")
  .on("message", ({ payload, userId }) => render(payload))
  .on("*", (message) => console.log(message.event))

await room.subscribe()
room.send("message", { text: "Hello" })

A broadcast goes to every other subscriber of the channel. The sender does not receive its own message.

Presence, online status, typing indicators

room.onPresence((event) => {
  if (event.event === "sync") setMembers(event.members)       // current list, sent when you join
  if (event.event === "join" || event.event === "update") …   // someone arrived or changed state
  if (event.event === "leave") …                              // someone left or lost connection
})

room.track({ name: "Ada", typing: false })
room.track({ name: "Ada", typing: true })    // update
room.untrack()

A member is removed automatically when its connection drops.

Database changes

Subscribe to db:<table> to hear about rows changed through the FASTBASE data API (fastbase.from(...)):

await fastbase.channel("db:messages")
  .on("INSERT", ({ payload }) => append(payload))
  .on("UPDATE", ({ payload }) => replace(payload))
  .on("DELETE", ({ payload }) => remove(payload))
  .subscribe()

Rows your app writes with its own database driver (Prisma, pg…) do not produce events yet. To announce those, broadcast from your server:

POST /v1/realtime/broadcast
apikey: <secret key>

{ "channel": "orders", "event": "created", "payload": { "id": 42 } }

db: channels are read-only for clients, so nobody can forge a change event.

Good to know

  • Channels are private to a project. Two projects using the same channel name never see each other.

  • Channel names decide who may subscribe:

    Name Who can subscribe
    private:<anything> Signed-in users of your app only
    user:<user id> (and user:<user id>:…) That user only
    anything else Anyone holding the project’s public key

    Use user:<id> for per-user notifications. For open channels, do not broadcast secrets; send ids and let clients fetch details through your authorised routes.

  • Messages are limited to 32 KB (about 7 KB for server broadcasts) and are not stored. A client that is offline misses them.

  • The SDK reconnects automatically and re-subscribes.