Server Connections
If you prefer to watch the video, click below.
These functions manage and inspect the network connection between the Meteor client and server.
Meteor.status()
Get the current connection status. A reactive data source.
This method returns the status of the connection between the client and the server. The return value is an object with the following fields:
- connectedBoolean
True if currently connected to the server. If false, changes and method invocations will be queued up until the connection is reestablished.
- statusString
Describes the current reconnection status. The possible values are
connected
(the connection is up and running),connecting
(disconnected and trying to open a new connection),failed
(permanently failed to connect; e.g., the client and server support different versions of DDP),waiting
(failed to connect and waiting to try to reconnect) andoffline
(user has disconnected the connection).- retryCountNumber
The number of times the client has tried to reconnect since the connection was lost. 0 when connected.
- retryTimeNumber or undefined
The estimated time of the next reconnection attempt. To turn this into an interval until the next reconnection, use
retryTime - (new Date()).getTime()
. This key will be set only whenstatus
iswaiting
.- reasonString or undefined
If
status
isfailed
, a description of why the connection failed.
Instead of using callbacks to notify you on changes, this is a reactive data source. You can use it in a template or computation to get realtime updates.
Meteor.reconnect()
Force an immediate reconnection attempt if the client is not connected to the server.
This method does nothing if the client is already connected.
Meteor.disconnect()
Disconnect the client from the server.
Call this method to disconnect from the server and stop all live data updates. While the client is disconnected it will not receive updates to collections, method calls will be queued until the connection is reestablished, and hot code push will be disabled.
Call Meteor.reconnect to reestablish the connection and resume data transfer.
This can be used to save battery on mobile devices when real time updates are not required.
Meteor.onConnection(callback)
Register a callback to be called when a new DDP connection is made to the server.
Arguments
- callback Function
-
The function to call when a new DDP connection is established.
onConnection
returns an object with a single method stop
. Calling
stop
unregisters the callback, so that this callback will no longer
be called on new connections.
The callback is called with a single argument, the server-side
connection
representing the connection from the client. This object
contains the following fields:
- idString
A globally unique id for this connection.
- closeFunction
Close this DDP connection. The client is free to reconnect, but will receive a different connection with a new
id
if it does.- onCloseFunction
Register a callback to be called when the connection is closed. If the connection is already closed, the callback will be called immediately.
- clientAddressString
The IP address of the client in dotted form (such as
127.0.0.1
).If you’re running your Meteor server behind a proxy (so that clients are connecting to the proxy instead of to your server directly), you’ll need to set the
HTTP_FORWARDED_COUNT
environment variable for the correct IP address to be reported byclientAddress
.Set
HTTP_FORWARDED_COUNT
to an integer representing the number of proxies in front of your server. For example, you’d set it to1
when your server was behind one proxy.- httpHeadersObject
When the connection came in over an HTTP transport (such as with Meteor’s default SockJS implementation), this field contains whitelisted HTTP headers.
Cookies are deliberately excluded from the headers as they are a security risk for this transport. For details and alternatives, see the SockJS documentation.
Currently when a client reconnects to the server (such as after temporarily losing its Internet connection), it will get a new connection each time. The
onConnection
callbacks will be called again, and the new connection will have a new connectionid
.
In the future, when client reconnection is fully implemented, reconnecting from the client will reconnect to the same connection on the server: the
onConnection
callback won’t be called for that connection again, and the connection will still have the same connectionid
.
DDP.connect(url, [options])
Connect to the server of a different Meteor application to subscribe to its document sets and invoke its remote methods.
Arguments
- url String
-
The URL of another Meteor application.
Options
- reloadWithOutstanding Boolean
-
is it OK to reload if there are outstanding methods?
- headers Object
-
extra headers to send on the websockets connection, for server-to-server DDP only
- _sockjsOptions Object
-
Specifies options to pass through to the sockjs client
- onDDPNegotiationVersionFailure Function
-
callback when version negotiation fails.
To call methods on another Meteor application or subscribe to its data
sets, call DDP.connect
with the URL of the application.
DDP.connect
returns an object which provides:
subscribe
- Subscribe to a record set. See Meteor.subscribe.call
- Invoke a method. See Meteor.call.apply
- Invoke a method with an argument array. See Meteor.apply.methods
- Define client-only stubs for methods defined on the remote server. See Meteor.methods.status
- Get the current connection status. See Meteor.status.reconnect
- See Meteor.reconnect.disconnect
- See Meteor.disconnect.
By default, clients open a connection to the server from which they’re loaded.
When you call Meteor.subscribe
, Meteor.status
, Meteor.call
, and
Meteor.apply
, you are using a connection back to that default
server.
DDP.onReconnect(callback)
Register a function to call as the first step of reconnecting. This function can call methods which will be executed before any other outstanding methods. For example, this can be used to re-establish the appropriate authentication context on the connection.
Arguments
- callback Function
-
The function to call. It will be called with a single argument, the connection object that is reconnecting.