RefirebaseRefirebase

subscribe

Listen to real-time updates for a document or collection.

Usage

Use db.firestore.subscribe to fetch data and continue listening to it in real-time. It handles both single documents (via docId) and complex queries.

Subscribe to a query
import { db } from 'refirebase';

const unsubscribe = db.firestore.subscribe(
  'messages',
  (data) => {
    console.log('Real-time data:', data);
  },
  { where: { read: false }, limit: 20 },
  (error) => {
    console.error('Subscription error:', error);
  }
);

// Stop listening when done
// unsubscribe();

Parameters

ParamTypeDescription
collectionName*stringThe name of the collection.
callback*FunctionCalled with the new data whenever it changes. Receives an array of documents, a single document, or null.
optionsGetById | GetByConditionTarget a specific docId, or apply where, orderBy, limit, and startAfter clauses.
errorCallbackFunctionOptional callback for listener errors.

Return value

Returns

() => void

Returns a function that unsubscribes the listener when called.