Skip to content

API reference > @kontsedal/olas-realtime > createConnectionState

createConnectionState() function ​

Reactive connection-state signal — 'connected' | 'reconnecting' | 'offline' | 'unknown'.

Backed by RealtimeService.onConnectionChange?(...). If the consumer's transport doesn't implement that method, the returned signal stays at'unknown' for the lifetime of the controller — the hook has no way to observe connection state, so it reports that honestly rather than lying with 'connected' (T6.7). With a reporter, it starts optimistically at'connected' until the first change corrects it.

Every createConnectionState and onReconnect on one RealtimeService shares one onConnectionChange listener. The first live one subscribes, and the last one to dispose (or suspend) unsubscribes. One that starts while the listener is live begins at the transport's latest report, since the transport's own "current state" call came once, on that subscribe. With no report since the listener opened, it begins at 'connected'. A resume counts as a start: the state from before the suspend is dropped, because nothing listened while the controller was suspended.

Useful for "stale-during-disconnect" UIs and as a refetch trigger when the connection comes back up:

ts
const conn = createConnectionState(ctx)
const orders = bindQuery(ctx, ordersQuery)
ctx.effect(() => {
  if (conn.value === 'connected') {
    orders.invalidateAll()
  }
})

Signature:

typescript
export declare function createConnectionState(ctx: Ctx<RealtimeDeps>): ReadSignal<ConnectionState>;

Parameters ​

Parameter

Type

Description

ctx

Ctx<RealtimeDeps>

Returns:

ReadSignal<ConnectionState>

Released under the MIT License.