Browser apps

Browser apps

A web page calls Nova through the Gateway, with a short-lived token that your backend gets for it.

A web page can append to a stream, read it, and follow it live over Nova's HTTP API. Three things differ from a server. The page goes through a Gateway that allows its origin. It holds a short-lived token that your backend got for it, never an API key. And it appends one batch per request, because a browser cannot keep a producer session open. For servers, see How clients authenticate.

Before a page sends a token, the browser asks the Gateway whether the page's origin may call it. This is a CORS preflight, and the Gateway answers it:

The browser's preflight, from https://app.example.com
curl -s -i -X OPTIONS localhost:8080/v1/streams/apps/orders%2Feu/records \
  -H 'Origin: https://app.example.com' \
  -H 'Access-Control-Request-Method: POST' \
  -H 'Access-Control-Request-Headers: authorization, content-type'
HTTP/1.1 204 No Content
Access-Control-Allow-Headers: Authorization, Content-Type, Last-Event-ID
Access-Control-Allow-Methods: GET, POST, PATCH, DELETE, OPTIONS
Access-Control-Allow-Origin: https://app.example.com
Access-Control-Max-Age: 600
Vary: Origin

Allowed origins

The Gateway allows exactly the origins its operator lists, such as https://app.example.com. An origin is a scheme, a host, and a port, matched exactly, with no wildcards. A Gateway allows no origin until its operator lists one, and the quickstart's Gateway lists none.

For an allowed origin, the browser caches the answer for 600 seconds. A preflight from any other origin gets 403 with the body origin not allowed, and the browser stops there. A simple request from another origin, one that needs no preflight, gets its answer without CORS headers, so the browser hides it from the page. A node's own HTTP listener answers no preflight at all, so a page always calls a Gateway.

PUT is not among the allowed methods. Updating a bucket or a queue uses PUT, so do it from your backend.

Your operator lists the origins on the Gateway; see the Gateway.

Tokens for the page

Never send an API key to a browser. Anyone who opens the page can read it. Keep the key on your backend and give the page a token for the one stream it needs, for a few minutes:

Sign in, ask for a token for apps/orders/eu POST /v1/token/exchange (API key, bucket, key, ttl) token, expires_at_unix_ms, scope token, expires_at_unix_ms Append, read, subscribe with Authorization: Bearer token Web page Your backend Gateway
The API key stays on your backend. The page holds a token for one stream.

Your backend asks the exchange for bucket and key, with a short ttl_seconds. A token lasts 15 minutes by default and 1 hour at most. Revoking the key does not stop a token already given out, so a short lifetime limits what a leaked token can do.

The page sends the token in the Authorization: Bearer header, the only place Nova reads one. A token in the URL or in a cookie is not read. The TypeScript SDK's bearerFunc asks your code for a token before every call and says which stream the call names. Keep the token in memory and fetch a new one shortly before expires_at_unix_ms.

Following a stream

A subscription over HTTP is a stream of server-sent events. Each event carries a batch of records, and its id is the bookmark after them. When the token expires, the stream ends:

A subscription whose token lived 5 seconds
id: 1
data: {"records":[{"seq_num":0,"timestamp_ms":1795371366510,"body":"order 1017 placed"}],"bookmark":"1","tail":{"seq_num":1,"timestamp_ms":1795371366510}}

event: auth-expired
data: {}

The browser's EventSource cannot send an Authorization header, so it works only against a cluster that checks no credentials. With tokens, read the response with fetch and parse the events from its body. The TypeScript SDK's HTTP transport does this for you.

To resume, get a fresh token and reconnect, sending the last event's id as the Last-Event-ID header. The stream goes on with the next record, so none are lost or repeated. A Gateway that does not check tokens itself ends the stream without the auth-expired event; resume the same way. The SDK reconnects by itself when it sees the event. Server-sent events has the heartbeat and the other events.

Writing from a page

A producer session needs a request whose body and response flow at the same time, and browser fetch cannot do that. In a browser, the SDK's openStream throws SessionUnsupportedError before it sends anything. Use one-shot appends: one batch per request, of at most 1,000 records and 1 MiB.

Each one-shot append waits for its acknowledgement, a round trip per batch. Requests sent at the same time can be stored in any order. To keep your records in order, put them in one batch, or wait for each append before you send the next. See appends.

See also

On this page