Skip to main content

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

Key points:
  • 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

Always call this.ready() when not returning a cursor, otherwise the subscription will never be marked as ready.

Multiple Collections in One Publication

Subscribing to Data

Subscriptions are created on the client using Meteor.subscribe().

Basic Subscription

Subscription with Arguments

Stopping Subscriptions

Always stop subscriptions when you’re done with them to free up resources on both client and server.

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

Always validate publication arguments and never trust client input.

Field Filtering

Never publish sensitive fields:

Performance Tips

  1. Use field projection to limit data sent over the wire
  2. Limit published documents with reasonable query constraints
  3. Use indexes on MongoDB for publication queries
  4. Consider denormalization for frequently accessed data
  5. Use oplog tailing for better performance in production