What is DDP?
DDP (Distributed Data Protocol) is Meteor’s protocol for syncing data between client and server. It’s a simple, REST-like protocol for fetching structured data from a server, and receiving live updates when that data changes.DDP is to realtime web apps what REST is to traditional web apps - a standardized way to send data over the network.
Package Structure
DDP is implemented across three core packages:ddp
Main package that combines client and server
ddp-client
Client-side DDP implementation
ddp-server
Server-side DDP implementation (server-only)
Package Dependencies
Frompackages/ddp/package.js:
ddp-common Package
Shared code between client and server:packages/ddp-common/package.js
Core Concepts
The Connection
A DDP connection is established via WebSocket:Connection Implementation
Frompackages/ddp-client/common/livedata_connection.js:
DDP Messages
DDP uses JSON messages over WebSocket. Here are the core message types:Connection Messages
connect - Establish Connection
connect - Establish Connection
Client initiates connection:Server responds:
ping/pong - Heartbeat
ping/pong - Heartbeat
Keep connection alive:With IDs:
Method Messages
method - Call Server Method
method - Call Server Method
Client calls method:Server responds with result:Or with error:
updated - Method Complete
updated - Method Complete
Server signals method side effects are complete:This means all database changes from method “1” have been sent to the client.
Subscription Messages
sub - Subscribe to Data
sub - Subscribe to Data
Client subscribes:Server sends initial data via
added messages, then:unsub - Unsubscribe
unsub - Unsubscribe
Client unsubscribes:Server confirms:
Data Messages
added - Document Added
added - Document Added
Server tells client about new document:
changed - Document Updated
changed - Document Updated
Server tells client about changes:With removed fields:
removed - Document Deleted
removed - Document Deleted
Server tells client document was removed:
Publications and Subscriptions
Server: Publications
Define what data clients can subscribe to:Client: Subscriptions
Subscribe to published data:React Integration
Methods (RPC)
Server: Define Methods
Client: Call Methods
Latency Compensation
DDP implements optimistic UI through latency compensation:Stub Functions
Define client-side simulation:Connection Management
Connection States
Manual Connection Control
Advanced Features
Multiple DDP Connections
DDP Rate Limiting
Custom DDP Messages
Best Practices
Secure Publications
Secure Publications
Never trust the client - always validate on server:
Validate Method Arguments
Validate Method Arguments
Use
check package:Optimize Subscriptions
Optimize Subscriptions
Only subscribe to data you need:
Next: Full-Stack Development
Learn how to build complete full-stack applications with Meteor