folkwaysDocs
JavaScript SDK

Client resource management

Own views explicitly, bound local work and recover from resource pressure.

Service quotas and local SDK limits are independent. A client can exhaust a local resource even when the service has capacity.

Own only the views you need

Topic handles are lightweight addresses. Headless publishing and callback subscriptions do not retain implicit message history. Acquire a view lease before subscribing if initial replay or snapshots should populate the view.

TypeScript
import type { Realtime } from '@folkwaysio/realtime';

export async function showRoom(client: Realtime) {
  const room = client.topic('rooms/42');
  const messages = room.messages.open();
  const typing = room.values('typing').open();
  const sub = room.subscribe({ from: { last: 20 } });
  try {
    await sub.ready;
    console.log(messages.getSnapshot(), typing.getSnapshot());
  } finally {
    await sub.unsubscribe();
    messages.dispose();
    typing.dispose();
  }
}

Subscription readiness is not replay completion. Reactive bindings acquire ownership with view.subscribe(listener) and release it through the returned cleanup. Views on the same topic share active data. Opening a view alone does not request replay. Active message views have a one-tick remount grace period before final disposal releases history.

Local budgets

ResourceDefaultConfiguration
Pending publishes1,000 / 8 MiBpendingPublishes.maxCount / maxBytes
Inbound work per topic4,096 / 4 MiBinbound.perTopicCount / perTopicBytes
Inbound work across client65,536 / 32 MiBinbound.maxCount / maxBytes
Current state65,536 / 32 MiBinbound.stateCount / stateBytes
Iterator buffer per iterator256 / 2 MiBFixed
Aggregate iterator buffers8,192 / 8 MiBFixed
Active view leases1,024Fixed
Settled display history100 entries / 2 MiB per topic; 8 MiB across clientFixed
Inactive in-memory cursors1,024Fixed
Optional cache100 messages per topic; 2 MiB resident budget; 256 topicscache.messagesPerTopic / maxBytes

These are estimated retained-byte limits, not JavaScript heap guarantees. Pending optimistic messages have separate accounting and are not silently evicted to admit new sends. Settled display history may be evicted. Committed state and temporary snapshot assembly each have a ceiling; during atomic replacement both may coexist temporarily (up to twice the state budget in aggregate). The last complete state stays visible until the replacement commits. Persistent cache accounting includes encoding overhead and is separately bounded; use one owner per namespace.

TypeScript
import { Folkways } from '@folkwaysio/realtime';

export function createClient(token: string) {
  return new Folkways.Realtime({
    url: 'https://realtime.folkways.net',
    token,
    pendingPublishes: { maxCount: 500, maxBytes: 4 * 1024 * 1024 },
    inbound: { perTopicCount: 2048, stateBytes: 16 * 1024 * 1024 },
  });
}

Recover from overload

Acknowledged publishes reject with QueueFull; signals may be dropped. Reduce publishing pressure and handle ambiguous outcomes using stable idempotency keys. Iterator overflow throws QueueFull for the affected iterator only; aggregate pressure sheds the largest backlog first. Terminal topic errors discard buffered iterator items. Unsubscribe, client close and identity change finish iteration normally. A topic-set member ending does not terminate the set.

A shared cursor tracks callback delivery and enqueueing, not successful asynchronous iterator processing. Save an application-owned checkpoint and resume from it when necessary, subject to retained history. If an incoming current-state snapshot exceeds the budget, adjust the relevant limit or workload before resubscribing. Inactive cursor eviction may require an explicit starting position.

Inspect resource usage

TypeScript
import type { Realtime } from '@folkwaysio/realtime';

export function inspect(client: Realtime) {
  const d = client.diagnostics();
  console.log(d.activeTopics, d.viewLeases, d.pendingPublishes, d.inboundBytes);
}
CountersMeaning
activeTopics, subscriptionsActive topic and subscription counts
kvValues, signalValues, retainedStateBytesRetained current state
inboundCount, inboundBytesPending inbound work
pendingPublishes, pendingPublishBytesPending publications
cacheTopics, cacheBytes, inactiveCursorsCache and cursor usage
viewTopics, viewLeases, viewMessages, viewValues, viewMessageBytesActive view ownership and retained display data

Diagnostics are local and payload-free; byte estimates are not a heap profiler. Measure actual memory under representative workloads.

See migration, client options and local storage.

On this page