DDP Connections API
DDP (Distributed Data Protocol) is Meteor’s protocol for sending data over WebSockets. These APIs allow you to manage connections and create multi-server applications. Source:packages/ddp-client/common/livedata_connection.js
DDP.connect()
Connect to another Meteor server and return a connection object. Locus: AnywhereURL of another Meteor application to connect to.
Optional connection options.
Whether to reload if there are outstanding methods. Default: false.
Extra headers to send on the WebSocket connection (server-to-server DDP only).
Whether to automatically retry on connection failures. Default: true.
Callback when version negotiation fails.
Connection Object
The connection object returned byDDP.connect() has the same API as Meteor on the client:
connection.subscribe()
Subscribe to a publication on the remote server.connection.call() / connection.callAsync()
Call a method on the remote server.connection.apply() / connection.applyAsync()
Call a method with an array of arguments.connection.status()
Get the current connection status. A reactive data source. Returns: Object with status informationTrue if currently connected to the server.
Connection status: “connected”, “connecting”, “failed”, “waiting”, or “offline”.
Number of reconnection attempts made.
Estimated time of next reconnection attempt.
If failed, description of why the connection failed.
connection.reconnect()
Force an immediate reconnection attempt.connection.disconnect()
Disconnect from the server.connection.close()
Permanently close the connection.connection.userId()
Get the user ID on this connection. A reactive data source.Using Remote Collections
Access collections from a remote server:Server-to-Server Connections
Connect Meteor servers to each other:Connection Lifecycle Events
Listen to connection events:DDP.onReconnect()
Register a callback to be called on reconnect.Meteor.onConnection() (Server)
Register a callback to be called when a new DDP connection is made to the server. Locus: ServerThe function to call when a new DDP connection is established.
Connection Object Properties (Server)
Globally unique identifier for this connection.
Close this DDP connection. The client will reconnect.
Register a function to be called when this connection is closed.
The IP address of the client.
HTTP headers sent by the client in the initial WebSocket handshake.
Multi-Server Architecture Example
Best Practices
- Connection Pooling: Reuse connections instead of creating new ones for each request.
- Error Handling: Always handle connection failures gracefully.
- Security: Use authentication for server-to-server connections.
- Cleanup: Close connections when no longer needed.