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
| Param | Type | Description |
|---|---|---|
collectionName* | string | The name of the collection. |
callback* | Function | Called with the new data whenever it changes. Receives an array of documents, a single document, or null. |
options | GetById | GetByCondition | Target a specific docId, or apply where, orderBy, limit, and startAfter clauses. |
errorCallback | Function | Optional callback for listener errors. |
Return value
Returns
() => voidReturns a function that unsubscribes the listener when called.