Skip to content

Push to clients

A push is a small message sent to a browser, a mobile app or a field device that is listening over server-sent events (SSE). It tells a screen that something it shows has changed: “order 7 changed”, “a job was assigned to you”. Push is how a FlowCatalyst application keeps open screens current without polling.

Push is not part of the messaging pipeline. Events and dispatch jobs are durable work between systems, with retries and an audit trail. A push is a hint to a person’s screen, and losing one is recoverable by design.

A push says what changed, not what it now looks like:

{ "orderId": "ord_0HZXEQ5Y8JY5Z", "version": 7 }

The client then reads the order through the application’s ordinary API.

Why. The API applies the caller’s normal access checks every time, and it always returns the current state. If the push carried the order itself, every subscriber of the topic would see the full record whatever their permissions on it, a late or replayed push could overwrite newer state with older, and the push would have to fit a size limit (16 KiB by default, 64 KiB at most). A signal has none of those problems: a duplicate or stale signal only causes one extra read.

An application may still put state in a push when the data is fine for everyone allowed on the topic and fits the limit. That is its choice, per topic.

A push is written in the same transaction as the change it announces. It is sent only if that transaction commits, and every committed push reaches every stream subscribed to its topic, in log order, once, as long as the stream stays open or resumes within the retention window (24 hours at most). A client that was away for longer gets a reset and reloads.

Nothing is promised beyond that: no delivery to a device that is not listening (that is a mobile notification, which push does not do), no presence, no client-to-server messages, and no ordering across hosts.

flowchart LR
  uc["Use case<br/>(change + push,<br/>one transaction)"] --> ob[("Push outbox")]
  ob --> relay["Relay<br/>(one at a time)"]
  relay --> log[("Log<br/>PostgreSQL or Redis")]
  log --> hub1["Hub, instance 1"]
  log --> hub2["Hub, instance 2"]
  hub1 -- "SSE" --> c1["Browser"]
  hub2 -- "SSE" --> c2["Field device"]
  1. Publish. The application adds the push to its use case’s plan (or publishes it inside a transaction it owns). It is written as an outbox row in that transaction.
  2. Relay. One relay at a time, chosen by a lock, moves committed pushes from the outbox into the log and gives each a log id. Because it only ever sees committed rows, the ids follow commit order with no gaps. A database sequence assigned at insert time would not: two transactions can commit in the opposite order to the one they started in, and a client that has seen id 41 must never later be owed id 40.
  3. Fan out. Every instance’s hub follows the log and sends each push to its open streams whose topics match. Each push is encoded once and shared by every stream that receives it.

The log has two drivers behind one contract, chosen by the push.driver setting:

postgres (the default) redis
Log a table in the database a Redis stream (Redis 6.2+ or Valkey)
Instances woken by LISTEN/NOTIFY a blocking stream read
Extra service none Redis
When to choose it to start, and for moderate rates when push traffic is high enough to want it off the database

Both pass the same contract tests and conformance vectors, so applications and clients cannot tell them apart. Switching drivers changes the log ids, so every client gets one reset and reloads.

A topic is the address of a push:

{application}:{segment}(:{segment})*
orders:order:ord_0HZXEQ5Y8JY5Z
orders:store:12:dock
orders:user:prn_0HZXEQ5Y8JY5Z

The first segment is always the application’s code. Two applications’ topics therefore never collide, even on a host that serves many applications. A push also has a type (for example order.updated; reset, auth-expired, subscribed and names starting fw. are reserved) and a JSON object as its data.

Every push also carries the client (the tenant) it was written for. A stream belongs to one client and hears only that client’s pushes on its topics, plus pushes written for the platform as a whole. Two clients whose topics happen to have the same name never hear each other.

Listening is two steps. The client first subscribes: it posts the topics it wants, with its usual credentials and any tickets, and gets back a short-lived stream token naming the topics it was granted. It then listens: it opens the stream with that token. A topic that is refused is reported as denied; it does not fail the others.

A topic is granted by a rule or by a ticket.

Rules are declared in the application’s code, per topic pattern, and kept as data:

Pattern Meaning
orders:order:{id} requiring orders:order:view any order of the stream’s client, for anyone holding the permission
orders:user:{principal} only the caller’s own topic
orders:announcements, declared public open to anyone, on purpose

{principal} must equal the caller’s principal id, {client} the caller’s client, and any other placeholder matches one segment. A rule with neither a {principal} placeholder nor a required permission is refused at start-up, so no topic is open by accident. Because rules are data rather than callbacks, a host other than the application (the platform’s gateway) can apply them.

Tickets cover what a rule cannot say, such as “this user may see order 7 but not order 8”. The application mints a ticket in its own endpoint, after its own access check, and the client presents it when it subscribes. A ticket is a JWS signed with the application’s Ed25519 key (EdDSA): it names the issuing application, the client, and the topics it grants, and it is valid for a few minutes, which is only the time to subscribe with it. It can be bound to a device key, so only a field device holding that key can use it. The host verifies tickets with the application’s public keys, published as a JWKS.

Why EdDSA. With an asymmetric signature, a host that only verifies, such as the platform gateway, never needs the application’s secret. Applications keep their private keys; the gateway holds public keys only.

Every push has a numeric id, sent as the SSE event id. A client keeps the last id it saw and sends it as Last-Event-ID when it reconnects; the host replays everything after it, from memory when it can and from the log otherwise.

  • Reset. If the client’s id is older than the log keeps, unknown to the host, or malformed, the host sends a reset event instead. The client reloads its state from the API and carries on. A host never starts such a stream live without telling the client.
  • Expiry. A stream token lives no longer than the credential behind it, and at most an hour by default. At expiry the host sends auth-expired and ends the stream; the client subscribes again and resumes with its last id, losing nothing.
  • Revocation. Revoking a field worker’s login or device ends that worker’s open streams on every instance. On the platform gateway, signing out of the platform does the same for the user’s streams. Other credential changes take effect at the next subscribe.
  • Back-pressure. Each stream has a bounded buffer. A client that falls too far behind is disconnected and resumes from its last id like any other drop, so one slow client never slows the others.
sequenceDiagram
  participant C as Client
  participant H as Host
  C->>H: POST /push/subscriptions (topics, credentials, tickets)
  H-->>C: streamToken, granted, denied
  C->>H: GET /push/stream?st=… (Last-Event-ID: 1041)
  H-->>C: subscribed {latest}
  H-->>C: replay 1042, 1043 …
  H-->>C: live pushes
  Note over C,H: on a drop, reconnect with the last id
  H-->>C: auth-expired at token expiry: subscribe again, resume

A screen usually loads its state and then starts listening. A change committed between the two would be missed. To close that gap, the read endpoint returns a push cursor alongside the state, and the client opens its first listen with the cursor as Last-Event-ID.

The rule is to read the cursor before reading the state. Every push at or below the cursor was logged before the read began, and the relay logs a push only after its transaction committed, so the change behind it is visible to the read. Any change the read lacks therefore has a push above the cursor, which the client receives. A cursor that lags the log is still correct: it only replays a few extra signals, and replaying a signal is harmless.

An application that publishes through the platform gateway has no local log, so it has no cursor to hand out. Its client opens the first listen live and reads its state once more when the stream reports subscribed, which closes the same gap from the client side.

Who hosts the streams: the application or the platform

Section titled “Who hosts the streams: the application or the platform”

There are two ways to host push. The wire (the routes, bodies and events) is the same in both, so the client libraries work against either by changing their base URL.

App-hosted Platform gateway
Who runs it the application, with the framework’s push module (Java or Go) fc-server, with FC_PUSH_ENABLED
Log the application’s database (or Redis) the platform’s database (or Redis)
How a push gets there written straight into the module’s push outbox a PUSH row in the application’s outbox_messages, relayed by the platform’s outbox processor
Who it suits framework services with their own long-running servers applications that cannot hold streams (PHP-FPM, serverless, functions), and devices that listen to several applications on one stream
Topics on one stream one application’s several applications’, with each application’s rules and tickets checked separately

Why both. Holding thousands of long-lived connections needs a long-running server with spare memory. A framework service has one, and hosting push itself keeps the push next to its data and adds no hop. A PHP application or a short-lived process does not, and a field device running two applications would otherwise need two streams. The gateway serves those cases from one place, with one console view, without inventing a second protocol: it runs the same framework module, configured for many applications. A framework application moves between the two by changing one setting (push.driver=platform); its code does not change.

flowchart LR
  app["Application<br/>(any SDK)"] -->|"PUSH row, same transaction"| aob[("outbox_messages")]
  aob --> proc["Outbox processor"]
  proc -->|"POST /api/push/batch"| gw["Push gateway"]
  gw --> glog[("Log")]
  glog --> dev["Browsers, apps,<br/>field devices"]
  app -. "rules + public ticket keys<br/>(definition sync)" .-> gw
  • Publishing. The SDK writes a PUSH row in the application’s transaction, as it writes events. By default the row’s message group is the topic, so the pushes of one topic arrive in order. On PostgreSQL the insert also notifies the outbox processor, which wakes on the commit instead of waiting for its next poll.
  • Ingest. POST /api/push/batch checks each item: the topic belongs to the caller’s application, the caller may write for the client, and the topic, type and data are valid. A duplicate push id is accepted once. The answer has one result per item, like the other batch endpoints.
  • Rules and keys are synced from the application’s code (POST /api/applications/{appCode}/push/sync), like its roles and event types: a full replace of the rules and the public ticket keys. Every gateway task reloads them within about a second.
  • Subscribers authenticate with platform credentials: a bearer access token, the platform session cookie (with its CSRF rule), or a field token with its DPoP proof. Ticket-only subscribes work as well.
  • Field workers are confined to their own application. A field token is issued for one application. The gateway grants such a token only that application’s non-public topics, by rule or by ticket; tickets from other applications are dropped. Each DPoP proof is accepted once.
  • Clients. A user who can act for several clients names one in the subscribe. Hearing every client’s pushes requires asking for the platform client explicitly and holding platform:push:stream:all-clients; no stream becomes platform-wide by default.
  • Isolation. Each application has its own limits on open streams, publish rate and data size, and can be paused. A paused or over-rate application’s batch is answered 429 and waits, in order, in its own outbox: nothing is lost and no retry attempts are spent.

A gateway can run inside the platform’s API process or as its own service from the same image. A standalone gateway validates platform and field tokens itself, so it needs the platform’s signing keys. With the database key store it reads them from the same keyring as the platform and reloads them every 30 seconds, so it needs FLOWCATALYST_APP_KEY and the database; with FC_JWT_KEY_STORE=config it needs the configured key. It refuses to start without keys.