Skip to main content

Installation

Find the anon key with npx @insforge/cli secrets get ANON_KEY, or in the dashboard: click Install and open API Keys.

Mental model

The TypeScript SDK opens one Socket.IO connection to your InsForge backend. You subscribe to named channels, listen for event names, and optionally publish events back to channels you have joined. Events can come from two places:
  • Database triggers that call realtime.publish(channel, event, payload).
  • Clients that call insforge.realtime.publish(channel, event, payload).
For the backend channel and RLS model, see Realtime overview.

Quick start

Register connect, disconnect, connect_error, and error handlers before calling connect() so early connection failures are visible.

connect()

Establish a WebSocket connection.
Returns:
Notes:
  • The SDK includes the current auth token when one exists. If there is no signed-in user, it can use the configured anon key.
  • Multiple connect() calls while a connection is already in progress reuse the same connection promise.
  • The connection attempt times out after 10 seconds.

subscribe()

Subscribe to a channel and receive the current presence snapshot.
Parameters: Returns:
subscribe() auto-connects if needed. Calling connect() explicitly is still recommended so connection event handlers are already attached. subscribe() is idempotent: calling it again for a channel you already joined re-requests the subscription and resolves the server’s current presence snapshot. The server tracks presence per logical member, so repeated subscribes never produce duplicate members or spurious presence:join events.

publish()

Publish an event to a channel.
Parameters:
Publishing requires a prior successful subscription to the same channel. If RLS is enabled on realtime.messages, publish is also checked against INSERT policies.
Publish failures are emitted through the error event.

on()

Listen for custom events, connection events, presence events, and realtime errors.
Reserved events:

once()

Listen for an event once, then remove the listener automatically.

off()

Remove an event listener.

unsubscribe()

Leave a channel.
unsubscribe() is fire-and-forget. If this was the final socket for a logical member, other subscribers receive presence:leave.

disconnect()

Close the WebSocket and clear local subscriptions.

Message shape

Delivered messages include your payload fields plus server metadata.
Metadata:
senderType is system for database-triggered messages and user for client-published messages.

Presence

A successful subscription returns the current presence snapshot.
Presence member:
Listen for changes:

Reading presence state

The SDK maintains the member list for every subscribed channel — seeded from the subscribe snapshot and kept current from presence:join/presence:leave deltas and reconnect resyncs. Read it at any time instead of merging deltas yourself:
Returns an empty array for channels you are not subscribed to. While the socket is briefly disconnected it holds the last known state, replaced by a fresh snapshot as soon as the channel resubscribes.

Reconnects

When the connection drops, the SDK automatically resubscribes to every channel on reconnect and emits presence:sync with the fresh presence snapshot for each. Members who joined or left while you were disconnected produce no individual presence:join/presence:leave deltas, so replace — don’t merge — any state you keep outside the SDK:
If you render directly from getPresenceState(), presence:sync is simply your re-render signal. If a resubscribe is rejected (for example the user’s access was revoked while disconnected), the SDK drops the channel and emits error instead.

Token refreshes

When the signed-in user’s access token refreshes, the SDK leaves an established socket connected — there is no in-band re-authentication, reconnect, or presence churn. A later initial connection or network reconnect obtains the latest access token before its handshake. Sign-in, sign-out, or switching users reconnects the socket under the new identity. Access reductions apply lazily on a live connection. Publishing is re-checked against RLS on every message, and new subscribes are re-checked at join time. Continued receipt on already-joined channels persists until the client unsubscribes or reconnects.

Properties

Error handling

Common realtime error codes:

Complete example