Realtime and utility services
Realtime and utility services give an application live data updates and small APIs for common display needs. Realtime channels deliver selected events over a shared connection. Presence publishes current activity. Locale provides regional data. Avatars return generated or fetched image assets.
Service landscape
These services use the configured client as their project and request context. Realtime uses a long-lived connection because events can arrive at any time. Locale and avatar requests use short HTTP calls because each result answers one focused question.
| Service | Main purpose | Typical result |
|---|---|---|
Realtime | Deliver selected application events as they occur | Event callbacks |
Presences | Read and manage short-lived user activity state | Presence records and lists |
Locale | Provide location context and regional catalogs | Locale objects and lists |
Avatars | Create or fetch display-ready visual assets | Image bytes or image URLs |
The configured client supplies the project, endpoint, session, and request transport that these services share. Realtime subscriptions and presence covers live updates. Locale and avatars covers the request-based utility APIs.
Realtime subscriptions
The Realtime service lets an application subscribe to one or more channels with optional query filters. A channel identifies the event scope. A query narrows the matching data. The service returns a subscription handle that can receive events, change its scope, or stop only that subscription.
Each subscription keeps its own channel and query selection. Its update action replaces those selections on the server without creating another subscription. Its unsubscribe action removes only that subscription. The shared connection remains available for other subscriptions, while disconnect is a separate operation that removes all active realtime work.
The connection waits for the server's application-level connected signal before it sends subscriptions or presence. It can authenticate with the configured session, restore active subscriptions after a temporary loss, and use heartbeats to keep the connection active. Open, close, and error callbacks let an application reflect connection state. Concurrent callers share one connection attempt instead of opening duplicate sockets.
The diagram shows how a channel choice becomes a routed application event.
sequenceDiagram
participant App as Application
participant Realtime as Realtime service
participant Server as Realtime server
App->>Realtime: Select channels and queries
Realtime->>Server: Open project connection
Server-->>Realtime: Connected
Realtime->>Server: Authenticate and register subscriptions
Server-->>Realtime: Matching event
Realtime-->>App: Invoke matching callback
App->>Realtime: Update or remove subscription
See Realtime subscriptions and presence for channel selection, event delivery, and subscription lifecycle details.
References: src/…/realtime.ts• L7-29 src/…/realtime.ts• L92-173 src/…/realtime.ts• L175-343 src/…/realtime.ts• L391-487 src/…/realtime.ts• L512-672 src/…/realtime.ts• L739-847 src/client.ts• L129-223 src/client.ts• L575-869
Subscription lifecycle
The subscription lifecycle keeps a caller's event scope independent from other callers. Creating a subscription stores its channel and query selection and starts the shared connection. The service sends it when the connection is ready. The returned handle can update or remove that subscription, while close retains the older behavior of also closing the connection when it was the last active subscription.
References: src/…/realtime.ts• L12-29 src/…/realtime.ts• L391-487 src/…/realtime.ts• L512-672 src/client.ts• L872-927
Presence publication
Realtime presence is the fast path for publishing a user's latest activity over the same live connection. A presence payload contains a status and presence identity, with optional permissions and metadata. The service keeps only the newest pending payload until the server connection is ready.
When the connection becomes ready, the service sends the pending presence after it restores subscriptions. A later reconnect sends the latest payload again. This path complements the record-based Presences API, which provides a readable state that a client can fetch after a missed event or a new connection.
See Realtime subscriptions and presence for the presence record model, expiry behavior, caching, and the relationship between live publication and record reads.
References: src/…/realtime.ts• L60-78 src/…/realtime.ts• L674-719 src/…/realtime.ts• L721-737 src/…/realtime.ts• L760-801 src/client.ts• L717-750
Locale data
The Locale service separates detected regional context from reusable catalogs. The location request returns IP-based country, continent, currency, and related context. Catalog requests provide standard values for language codes, locale codes, countries, phone codes, currencies, and continents.
This split supports two common flows. An application can use detected context for defaults. It can use stable codes and localized names for forms, filters, and stored values. The client's locale setting controls the language of supported catalog names.
| Data group | Product use |
|---|---|
| Current location | Choose regional defaults from the request context |
| Locale and language codes | Build language and locale choices |
| Countries and continents | Build geographic choices and filters |
| Country phone codes | Build phone number forms |
| Currencies | Display money choices and regional currency data |
See Locale and avatars for the locale response fields and catalog mapping.
References: src/…/locale.ts• L6-51 src/…/locale.ts• L53-131 src/…/locale.ts• L133-211 src/…/locale.ts• L213-371 src/…/locale.ts• L373-453 src/…/locale.ts• L456-539 src/…/locale.ts• L541-621 src/client.ts• L461-480
Avatar assets
The Avatars service turns small inputs into image resources for common interface and sharing needs. It covers browser and payment icons, country flags, initials, profile photos, QR images, remote image resizing, favicons, and web page screenshots. Each asset family supports binary image results, and the families that are useful in direct page loads also expose URL builders.
Profile photo resolution follows the identity supplied by the caller. A userId resolves sources known for that user. An explicit emailHash or name overrides that part of the lookup. Without a userId, those explicit values resolve an independent avatar and the current session user does not replace it. Email input is accepted only as a SHA-256 hash, so a raw email address does not enter an avatar URL.
The service also supports the authenticated user's photo lifecycle. An uploaded photo takes priority over other sources. Deleting it stores the built-in placeholder until a new photo is uploaded. Large uploads report progress while the service sends the file in parts, with limited parallel work for the remaining parts.
Screenshot requests add browser context to the asset request. The application can set the viewport, theme, locale, time zone, location, permissions, wait time, and full-page capture. It can request a fixed image format or use auto so the service selects AVIF or WebP when the request accepts them and falls back to PNG. Remote favicon and image requests do not follow HTTP redirects, so their source URLs must serve the asset directly.
See Locale and avatars for asset families, screenshot context and output formats, profile photo resolution, upload behavior, remote URL limits, and binary-versus-URL use.
References: src/…/avatars.ts• L15-148 src/…/avatars.ts• L282-353 src/…/avatars.ts• L355-480 src/…/avatars.ts• L482-589 src/…/avatars.ts• L591-719 src/…/avatars.ts• L721-895 src/…/avatars.ts• L897-1223 src/…/avatars.ts• L1226-1349 src/…/avatars.ts• L1351-1708 src/…/avatars.ts• L1729-2357 src/client.ts• L937-1077
Change checklist
When these files or sections are changed, remember to consider these:
- When realtime event or subscription fields change, check the channel input, server event envelope, subscription ID routing, callback contract, and the child page together.
- When connection readiness or reconnect behavior changes, preserve the order between the server connected signal, authentication, subscription registration, and presence publication.
- When subscription update or teardown changes, keep per-subscription changes and removal separate from full client shutdown so unrelated callbacks remain active.
- When presence fields or resend rules change, keep the queued latest payload aligned with reconnect behavior and the record-based presence contract.
- When locale record shapes change, keep each catalog operation aligned with its returned model and with the stable codes used by callers.
- When an avatar input or output changes, keep binary requests and URL builders aligned for the same asset family.
- When profile photo source rules change, keep
userId,emailHash, andnamebehavior aligned with the rule that raw email addresses are not accepted. - When screenshot output negotiation changes, keep the
outputoptions and accepted image media type aligned for both binary requests and URL use.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A subscription or presence message is sent before the server's connected signal | The server can reject the message and close the connection | Wait for the application-level connected state |
| A feature closes the shared connection while another feature has subscriptions | Other callbacks and queued presence updates stop | Use unsubscribe for one subscription and disconnect only for full client teardown |
| A pending presence payload is treated as durable record state | The application can show stale activity after a missed update | Use the record-based presence API when the current state must be read again |
| A locale consumer treats optional location fields as required | Missing regional data can produce invalid display or form values | Use required identity fields first and check optional fields before use |
| A remote avatar source requires an HTTP redirect | Favicon or image retrieval does not reach the final asset | Pass a URL that serves the asset directly |
| Image bytes are handled as structured data | The application cannot display the generated asset | Use the binary result as image data or use the matching URL builder |
| A profile photo request sends a raw email address | Personal data can enter an avatar URL or external lookup | Send only the SHA-256 hash in emailHash |
| A screenshot requests adaptive formats without an image-capable accept type | The response may not use the requested AVIF or WebP format | Use output: auto with an accept type that includes supported images or choose a fixed format |
Key files
| File | Why to read it |
|---|---|
| src/client.ts | Defines shared project and session context, HTTP requests, and the realtime transport used by the services. |
| src/…/realtime.ts | Defines channel subscriptions, subscription updates and teardown, connection state, reconnect behavior, event callbacks, and live presence publication. |
| src/…/presences.ts | Defines record-based presence reads, writes, expiry, and cache controls. |
| src/…/locale.ts | Defines detected location data and regional catalog requests. |
| src/…/avatars.ts | Defines generated assets, profile photo resolution, photo updates, screenshots, binary results, adaptive image output, and URL builders. |