subscribe method

void subscribe(
  1. String collectionName, {
  2. required String filterExpr,
  3. List args = const [],
})

Subscribe to server-side filtered updates for a collection.

This tells the server to only send changes matching the specified filter, reducing network traffic and improving performance. The filter should match your local Realm query to ensure consistency.

Parameters:

  • collectionName: The MongoDB collection to subscribe to
  • filterExpr: Realm Query Language filter (e.g., 'status == $0 AND userId == $1')
  • args: Values for filter placeholders in order

Example:

// Subscribe to only active tasks for current user
realmSync.subscribe(
  'tasks',
  filterExpr: 'status == \$0 AND ownerId == \$1',
  args: ['active', currentUserId],
);

Important: Call this before or after start(). Server will only send changes matching this filter for the specified collection.

Implementation

void subscribe(
  String collectionName, {
  required String filterExpr,
  List<dynamic> args = const [],
}) {
  _subscriptionsByCollection[collectionName] = {
    'filter': filterExpr,
    'args': args,
  };
  socket.emit('sync:subscribe', {
    'collection': collectionName,
    'filter': filterExpr,
    'args': args,
  });
}