Skip to content

RealtimeChannel

//fluxbase-kotlin/io.github.nimbleflux.fluxbase.realtime/RealtimeChannel

class RealtimeChannel(baseUrl: String, channelName: String, token: String?, transport: WebSocketTransport, coroutineDispatcher: CoroutineContext = Dispatchers.IO)

A realtime channel — one WebSocket connection to /realtime?token=<jwt> that subscribes to postgres_changes, broadcast, and/or presence events.

Port of RealtimeChannel from sdk/src/realtime.ts.

The channel manages its own WebSocket lifecycle:

  • On subscribe: connects, sends {type:"subscribe", channel, config} per postgres_changes config, starts a 30s heartbeat.
  • On postgres_changes messages: normalizes new_record→new / old_record→old (matching the Supabase shape) and dispatches to callbacks.
  • On broadcast messages: dispatches to event-specific callbacks.
  • Reconnects with exponential backoff (capped at 10 attempts).

jvm

baseUrl the Fluxbase base URL (http(s)→ws(s)).
channelName the channel identifier.
token the JWT (sent as ?token= query param, not subprotocol).
transport the WebSocketTransport SPI (tests inject a fake).
RealtimeChannel [jvm]
constructor(baseUrl: String, channelName: String, token: String?, transport: WebSocketTransport, coroutineDispatcher: CoroutineContext = Dispatchers.IO)
Name Summary
isSubscribed [jvm]
val isSubscribed: Boolean
Whether this channel is actively subscribed.
onError [jvm]
var onError: (JsonObject) -> Unit?
Optional callback invoked when the server sends an error message. Set this to surface subscription/server errors to application code.
Name Summary
broadcast [jvm]
suspend fun broadcast(event: String, payload: Map<String, Any?>)
Broadcast a message on this channel. Payload values encode as real JSON.
on [jvm]
fun on(event: String, config: PostgresChangesConfig? = null, callback: ChangeEventCallback): RealtimeChannel
Register a callback for a postgres_changes event type. Port of `channel.on(“INSERT”
onBroadcast [jvm]
fun onBroadcast(event: String, callback: BroadcastCallback): RealtimeChannel
Register a callback for broadcast events. Port of channel.on("broadcast", {event}, cb) in realtime.ts.
subscribe [jvm]
suspend fun subscribe()
Subscribe to the channel — connects the WebSocket and sends subscribe messages. Port of subscribe() in realtime.ts:540-617.
unsubscribe [jvm]
suspend fun unsubscribe()
Unsubscribe and close the connection. Port of unsubscribe() in realtime.ts.
updateToken [jvm]
suspend fun updateToken(newToken: String)
Update the auth token (e.g. after a JWT refresh). If the socket is open, pushes an access_token message so the server refreshes auth context without a reconnect; otherwise the new token is used on the next connect.