Overview
Meteor uses a publish-subscribe pattern for data loading, built on the Distributed Data Protocol (DDP). This allows the server to push data to clients in real-time, keeping client-side collections synchronized with the server.Publications and Subscriptions
In traditional HTTP-based applications, the client makes requests and receives responses. Meteor’s DDP allows bidirectional data flow:- Publication: A named API endpoint on the server that constructs and sends data to clients
- Subscription: A client connection to a publication that receives data and updates
A subscription creates a “pipe” that connects a server-side MongoDB collection to the client-side Minimongo cache, keeping them synchronized in real-time.
Defining Publications
Publications should be defined in server-only files.Basic Publication
- Return a MongoDB cursor to publish data
- Use field projection to limit exposed data
- The publication name is used by clients to subscribe
Publication with Parameters
Using this.userId
Multiple Collections in One Publication
Subscribing to Data
Subscriptions are created on the client usingMeteor.subscribe().
Basic Subscription
Subscription with Arguments
Stopping Subscriptions
Reactive Subscriptions
In Blaze Templates
In React with useTracker
Fetching Data
Always Use Specific Queries
Fetch Near Subscribe
Keep data fetching close to subscriptions to avoid “action at a distance”:Advanced Publication Patterns
Reactive Joins
Publish related data from multiple collections:Low-Level Publish API
For complete control over what data is sent:Counting Records Efficiently
Publication Strategies
Meteor supports different publication strategies to balance performance and memory usage:Subscription Readiness
Single Subscription
Multiple Subscriptions
In Blaze Templates
Security Considerations
Field Filtering
Never publish sensitive fields:Performance Tips
- Use field projection to limit data sent over the wire
- Limit published documents with reasonable query constraints
- Use indexes on MongoDB for publication queries
- Consider denormalization for frequently accessed data
- Use oplog tailing for better performance in production