MongoRealtime class

A client for MongoDB realtime updates over WebSocket.

Use this object to subscribe to query results, perform CRUD operations, and emit custom realtime events to the server.

Constructors

MongoRealtime(String url, {WarningHandler? warningHandler, Object? authData, RealtimeReconnectConfig? autoReconnectConfig, ConnectedHandler? onConnected, DisconnectedHandler? onDisconnected, RealtimeStorageAdapter? storageAdapter, RealtimeCachePolicy? cachePolicy, RealtimeHeartbeatConfig? heartbeatConfig})
Creates a new MongoRealtime client connected to the given WebSocket URL.
factory

Properties

autoReconnectConfig → RealtimeReconnectConfig
Returns the reconnect configuration used by this client.
no setter
cachePolicy → RealtimeCachePolicy
Returns the current local cache policy.
no setter
hashCode → int
The hash code for this object.
no setterinherited
heartbeatConfig → RealtimeHeartbeatConfig
Returns the heartbeat configuration.
no setter
isConnected → bool
Returns whether the websocket is currently connected.
no setter
isDisposed → bool
Returns whether the client is disposed.
no setter
isPaused → bool
Returns whether the client is currently paused (e.g. backgrounded).
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
url → String
Returns the WebSocket URL currently used by this client.
no setter
watch ↔ DbWatcher
latefinal

Methods

collection<T>(String name, {FromJson<T>? fromJson, String? idField}) → RealtimeCollectionReference<T>
Returns a reference to the named collection.
delete(String collection, {required JsonMap filter, bool optimistic = false}) → Future<void>
Deletes documents from collection matching filter.
disconnect() → Future<void>
Closes the websocket connection without disposing the client. Needs manual reconnect() call.
dispose() → Future<void>
Disposes the client, closes all subscriptions, and disconnects the underlying WebSocket connection.
emit(String event, [Object? payload]) → Future<Object?>
Emits a custom realtime event to the server with an optional payload, and awaits an optional response.
insert(String collection, JsonMap document, {bool optimistic = false}) → Future<void>
Inserts a new document into the specified collection.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pause() → Future<void>
Pauses the client (typically when the mobile app enters background).
reconnect({String? url, Object? authData}) → Future<void>
Reconnects the client to the server.
resume() → Future<void>
Resumes the client (typically when the mobile app returns to foreground).
toString() → String
A string representation of this object.
inherited
update(String collection, {required JsonMap update, required JsonMap filter, bool optimistic = false}) → Future<void>
Updates documents in collection matching filter using MongoDB update operators from update.

Operators

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

Static Properties

client → MongoRealtime
Returns the shared client created by the last call to MongoRealtime.connect.
no setter
isInitialized → bool
Returns true if the shared client has been initialized.
no setter

Static Methods

connect(String url, {bool autoLifecycle = true, bool persistToDisk = true, WarningHandler? warningHandler, Object? authData, RealtimeReconnectConfig? autoReconnectConfig, ConnectedHandler? onConnected, DisconnectedHandler? onDisconnected, RealtimeStorageAdapter? storageAdapter, RealtimeCachePolicy? cachePolicy, RealtimeHeartbeatConfig? heartbeatConfig}) → Future<MongoRealtime>
Connects to the specified WebSocket url and returns a shared client instance.
initialize({required String url, bool autoLifecycle = true, bool persistToDisk = true, RealtimeStorageAdapter? storageAdapter, RealtimeCachePolicy? cachePolicy, RealtimeHeartbeatConfig heartbeatConfig = const RealtimeHeartbeatConfig(), RealtimeReconnectConfig autoReconnectConfig = const RealtimeReconnectConfig(), ConnectedHandler? onConnected, DisconnectedHandler? onDisconnected}) → Future<MongoRealtime>
Deprecated alias for connect.
registerFlutterHook(RealtimeFlutterHook hook) → void
Registers a platform/framework specific hook (e.g. Flutter).