BrowserRealtimeClient class

Browser-only BloomRealtimeChannelClient backed by native browser WebSockets (package:web).

Implements resilient, multiplexed realtime communication for Bloom JS Native applications:

  • Channel subscription multiplexing (multiple calls to subscribe share one underlying socket subscription).
  • Automatic presence tracking with snapshots, joins, and leaves.
  • Heartbeat ping/pong keep-alives (automatically replies to server pings with pong frames).
  • Bounded exponential backoff reconnection with jitter on unexpected socket disconnects.
  • Automatic channel and presence re-subscription upon reconnecting.

VM / SSR Safety

This class requires browser WebSocket APIs and must only be imported in client-side code via package:bloom_js_native/browser.dart. Do not import in server-side rendering (SSR) code.

Example

final client = BrowserRealtimeClient(
  uri: Uri.parse('wss://api.example.com/ws'),
);

final binding = BloomRealtimeBinding(client: client);
await client.connect();

final chatMessages = binding.channel('chat:general');
final activeUsers = binding.presence('chat:general', userInfo: {'name': 'Alice'});
Implemented types

Constructors

BrowserRealtimeClient({required Uri uri, Duration initialReconnectDelay = defaultRealtimeInitialReconnectDelay, Duration maxReconnectDelay = defaultRealtimeMaxReconnectDelay, double backoffMultiplier = defaultRealtimeBackoffMultiplier, bool autoReconnect = true, Duration pingInterval = defaultRealtimePingInterval, List<String>? protocols})
Creates a BrowserRealtimeClient connecting to uri.

Properties

autoReconnect → bool
Whether to automatically reconnect when the connection drops unexpectedly.
final
backoffMultiplier → double
Exponential backoff multiplier applied on each consecutive failed reconnection attempt.
final
hashCode → int
The hash code for this object.
no setterinherited
initialReconnectDelay → Duration
Initial reconnection delay before the first retry attempt.
final
isConnected → bool
Whether the client is currently connected to the server.
no setter
maxReconnectDelay → Duration
Maximum ceiling on reconnection delay.
final
onStateChanged → Stream<RealtimeConnectionState>
Broadcast stream that emits whenever state changes.
no setteroverride
pingInterval → Duration
Interval at which heartbeat ping keep-alive messages are sent. Set to Duration.zero to disable automatic client pings.
final
protocols → List<String>?
Optional WebSocket sub-protocols to negotiate with the server.
final
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
state → RealtimeConnectionState
The current connection state of the client.
no setteroverride
uri → Uri
Target WebSocket server endpoint URI.
final

Methods

broadcast(String channelName, Map<String, dynamic> payload) → void
Broadcasts a payload map to channelName.
connect() → Future<void>
Explicitly initiates connection to the WebSocket server.
dispose() → void
Closes the socket, cancels reconnect and ping timers, closes all broadcast controllers, and releases resources.
override
joinPresence(String channelName, Map<String, dynamic> userInfo) → Stream<List<Map<String, dynamic>>>
Joins presence in channelName publishing userInfo metadata.
override
leavePresence(String channelName) → void
Leaves presence in channelName, removing local presence state and sending a presence leave frame to the server.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
send(RealtimeMessage message) → bool
Sends a raw RealtimeMessage envelope across the WebSocket connection.
subscribe(String channelName) → Stream<Map<String, dynamic>>
Subscribes to channelName and returns a broadcast stream of message payloads.
override
toString() → String
A string representation of this object.
inherited
unsubscribe(String channelName) → void
Unsubscribes from channelName, closing its local broadcast controller and sending an unsubscribe frame to the server if connected.
override

Operators

operator ==(Object other) → bool
The equality operator.
inherited