Installation
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).
Quick start
connect()
Establish a WebSocket connection.- 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.
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.
Publish failures are emitted through the
error event.
on()
Listen for custom events, connection events, presence events, and realtime errors.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.senderType is system for database-triggered messages and user for client-published messages.
Presence
A successful subscription returns the current presence snapshot.Reading presence state
The SDK maintains the member list for every subscribed channel — seeded from the subscribe snapshot and kept current frompresence:join/presence:leave deltas and reconnect resyncs. Read it at any time instead of merging deltas yourself:
Reconnects
When the connection drops, the SDK automatically resubscribes to every channel on reconnect and emitspresence: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:
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.