Events and live updates
Poll or subscribe
events.list reads the event outbox with the public scope. events.subscribe opens a live stream with the stream scope. Both are scoped by your customer key, and both run on your backend. User sessions cannot open the stream.
A StreamEvent has seq, type, chainId, vaultAddress, occurredAt and data. occurredAt is Unix seconds. Sequence numbers increase within an API deployment. Keep checkpoints separate across environments and filter sets.
Use event types as invalidation signals, then reload the affected resource. A metadata event can refresh a vault card, for example. The business-event stream is not a raw blockchain log subscription and does not replace receipt confirmation.
Poll from a checkpoint
import { createT3trisClient } from "@t3tris-labs/sdk";
const client = createT3trisClient({ apiKey: process.env.T3TRIS_API_KEY });
export async function poll(after: number) {
const page = await client.events.list({ after, limit: 100 });
// Process and persist the events before committing the returned checkpoint.
const nextAfter = page.items.at(-1)?.seq ?? after;
return { events: page.items, nextAfter, nextCursor: page.nextCursor };
}
after is exclusive: the response contains events with seq > after. Follow nextCursor with the same query filters when draining a paginated result. Do not poll continuously without a delay, and back off on request failures or quota limits.
Consume a bounded live subscription
import { createT3trisClient, type StreamEvent } from "@t3tris-labs/sdk";
export async function consumeEvents(
since: number,
signal: AbortSignal,
handle: (event: StreamEvent) => Promise<void>,
) {
const client = createT3trisClient({ apiKey: process.env.T3TRIS_API_KEY });
for await (const event of client.events.subscribe({ since }, { signal })) {
await handle(event);
// Persist event.seq only after handle has completed successfully.
}
}
The SDK reconnects after unexpected disconnects with backoff and resumes from its current cursor. Breaking out of the loop closes the iterator. Abort the supplied signal on shutdown. A clean server close ends the iteration, while an authentication denial throws an ApiError; decide explicitly whether and when your service should subscribe again.
With { live: true } and no prior cursor, the stream starts at the current watermark instead of replaying history. Omit it when you need replay. The types array filters event types in both polling and streaming.
Persist checkpoints safely
onProgress(cursor) reports the server-acknowledged sequence, including watermarks that advance across filtered-out rows. onReady signals that a connection's initial replay/watermark has been established; it can fire again after reconnection:
import { createT3trisClient } from "@t3tris-labs/sdk";
const client = createT3trisClient({ apiKey: process.env.T3TRIS_API_KEY });
const controller = new AbortController();
let acknowledgedCursor = 0;
let ready = false;
const stream = client.events.subscribe(
{ since: acknowledgedCursor, types: ["vault.metadata.updated"] },
{
signal: controller.signal,
onProgress: (cursor) => { acknowledgedCursor = cursor; },
onReady: () => { ready = true; },
},
);
A progress callback can run before your asynchronous event handler finishes. For durable processing, keep an applied-event checkpoint separately and commit it transactionally with your work. Only persist a later watermark after all preceding delivered events have completed. Make handlers idempotent so a crash and replay cannot apply an action twice.
For web UIs, forward sanitized updates from your backend over your own SSE/WebSocket connection. The SDK obtains a short-lived stream ticket server-side; never place the durable API key in a browser WebSocket URL.
Upcoming: scoped browser grants
events.browserTicket is a server-only addition for the next SDK release; it is not available in the published 0.8.2 package. It is reserved for specially provisioned unlimited integration credentials with stream access, not ordinary customer keys. Customer integrations should continue using the backend subscription above.
A same-origin server endpoint supplies the browser's exact HTTP(S) origin, a stable server-verified ingress identity as clientId, and optional wallet address / current vault: { chainId, address } hints. Never trust an identity supplied by the browser or expose the integration credential. The method accepts an optional abort signal and returns a short-lived { ticket, expiresAt } grant.
The browser redeems the ticket once at the public API's /v0/stream WebSocket endpoint. The API binds redemption to the browser Origin and fixes the filters when issuing the ticket; URL parameters cannot widen them. Wallet filters cover positions and operated vaults, current-vault hints follow the existing known-address read policy, and delivery rechecks visibility and moderation. An empty filter receives only connection/watermark frames.
Frames are hello (with a cursor) or event (with a StreamEvent). Consider the connection ready only after hello. Save an acknowledged cursor and reconnect with a new ticket and since to resume; use live=1 without a cursor to start at the current watermark. Grants expire, sockets rotate after five minutes, and admission/issuance limits still apply. Bound reconnect backoff and retained messages, honor Retry-After, and render event data as text.