Interface RealtimeConnectionHandler<Output>

Options for connecting to the realtime endpoint.

interface RealtimeConnectionHandler<Output> {
    clientOnly?: boolean;
    connectionKey?: string;
    decodeMessage?: ((data: any) => any);
    encodeMessage?: ((input: any) => string | Uint8Array);
    maxBuffering?: number;
    path?: string;
    throttleInterval?: number;
    tokenExpirationSeconds?: number;
    tokenProvider?: TokenProvider;
    onClose?(event: {
        code: number;
        reason: string;
    }): void;
    onError?(error: ApiError<any>): void;
    onResult(result: Output & WithRequestId): void;
}

Type Parameters

  • Output

Properties

clientOnly?: boolean

If true, the connection will only be established on the client side. This is useful for frameworks that reuse code for both server-side rendering and client-side rendering (e.g. Next.js).

This is set to true by default when running on React in the server. Otherwise, it is set to false.

Note that more SSR frameworks might be automatically detected in the future. In the meantime, you can set this to true when needed.

connectionKey?: string

The connection key. This is used to reuse the same connection across multiple calls to connect. This is particularly useful in contexts where the connection is established as part of a component lifecycle (e.g. React) and the component is re-rendered multiple times.

decodeMessage?: ((data: any) => any)

Optional decoder for incoming messages. Defaults to msgpack with JSON support for string payloads.

encodeMessage?: ((input: any) => string | Uint8Array)

Optional encoder for outgoing messages. Defaults to msgpack. Should return either a Uint8Array (binary) or string (text frame).

maxBuffering?: number

Configures the maximum amount of frames to store in memory before starting to drop old ones for in favor of the newer ones. It must be between 1 and 60.

The recommended is 2. The default is undefined so it can be determined by the app (normally is set to the recommended setting).

path?: string

Optional path to append after the app id. Defaults to /realtime.

throttleInterval?: number

The throttle duration in milliseconds. This is used to throttle the calls to the send function. Realtime apps usually react to user input, which can be very frequent (e.g. fast typing or mouse/drag movements).

The default value is 128 milliseconds.

tokenExpirationSeconds?: number

The token expiration time in seconds. This is used to determine when to refresh the token. The token will be refreshed at 90% of this value.

Only relevant when using a custom tokenProvider. If a custom tokenProvider is used without specifying this value, automatic token refresh will be disabled.

tokenProvider?: TokenProvider

A custom token provider function. When provided, this function will be used to fetch authentication tokens instead of the default internal token fetching mechanism.

This is useful when you want to fetch tokens through your own backend proxy. If not provided, the default getTemporaryAuthToken will be used.

Methods

  • Called when the connection closes NORMALLY (code 1000) from the remote side. Abnormal closures keep reporting through onError. Optional and additive: consumers that treat this connection as a live session (a signaling ride-along, say) need to hear a clean remote goodbye that is not an error, or they keep reporting live over a socket that is gone.

    Parameters

    • event: {
          code: number;
          reason: string;
      }
      • code: number
      • reason: string

    Returns void

  • Callback function that is called when an error occurs.

    Parameters

    • error: ApiError<any>

      The error that occurred.

    Returns void

  • Callback function that is called when a result is received.

    Parameters

    • result: Output & WithRequestId

      The result of the request.

    Returns void