Server Connections

Documentation on how to use Meteor's client-server connection.

These functions manage and inspect the network connection between theMeteor client and server.

Client

Meteor.status()

import { Meteor } from 'meteor/meteor' (ddp-client/common/livedata_connection.js, line 875)

Get the current connection status. A reactive data source.

This method returns the status of the connection between the client andthe server. The return value is an object with the following fields:

  • connectedBoolean
  • True if currently connected to the server. If false, changes andmethod invocations will be queued up until the connection isreestablished.

  • statusString

  • Describes the current reconnection status. The possiblevalues are connected (the connection is up andrunning), connecting (disconnected and trying to open anew connection), failed (permanently failed to connect; e.g., the clientand server support different versions of DDP), waiting (failedto connect and waiting to try to reconnect) and offline (user has disconnected the connection).

  • retryCountNumber

  • The number of times the client has tried to reconnect since theconnection was lost. 0 when connected.

  • retryTimeNumber or undefined

  • The estimated time of the next reconnection attempt. To turn thisinto an interval until the next reconnection, useretryTime - (new Date()).getTime(). This key willbe set only when status is waiting.

  • reasonString or undefined

  • If status is failed, a description of why the connection failed.

Instead of using callbacks to notify you on changes, this isa reactive data source. You can use it in atemplate or computationto get realtime updates.

Client

Meteor.reconnect()

import { Meteor } from 'meteor/meteor' (ddp-client/common/livedata_connection.js, line 888)

Force an immediate reconnection attempt if the client is not connected to the server.

This method does nothing if the client is already connected.

Client

Meteor.disconnect()

import { Meteor } from 'meteor/meteor' (ddp-client/common/livedata_connection.js, line 899)

Disconnect the client from the server.

Call this method to disconnect from the server and stop alllive data updates. While the client is disconnected it will not receiveupdates to collections, method calls will be queued until theconnection is reestablished, and hot code push will be disabled.

Call Meteor.reconnect to reestablish the connectionand resume data transfer.

This can be used to save battery on mobile devices when real timeupdates are not required.

Server

Meteor.onConnection(callback)

import { Meteor } from 'meteor/meteor' (ddp-server/livedata_server.js, line 1431)

Register a callback to be called when a new DDP connection is made to the server.

Arguments

  • callbackFunction
  • The function to call when a new DDP connection is established.

onConnection returns an object with a single method stop. Callingstop unregisters the callback, so that this callback will no longerbe called on new connections.

The callback is called with a single argument, the server-sideconnection representing the connection from the client. This objectcontains the following fields:

  • idString
  • A globally unique id for this connection.

  • closeFunction

  • Close this DDP connection. The client is free to reconnect, but willreceive a different connection with a new id if it does.

  • onCloseFunction

  • Register a callback to be called when the connection is closed. If theconnection 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 clientsare connecting to the proxy instead of to your server directly),you’ll need to set the HTTP_FORWARDED_COUNT environment variablefor the correct IP address to be reported by clientAddress.

Set HTTP_FORWARDED_COUNT to an integer representing the number ofproxies in front of your server. For example, you’d set it to 1when your server was behind one proxy.

  • httpHeadersObject
  • When the connection came in over an HTTP transport (such as withMeteor’s default SockJS implementation), this field containswhitelisted HTTP headers.

Cookies are deliberately excluded from the headers as they are asecurity risk for this transport. For details and alternatives, seethe SockJSdocumentation.

Currently when a client reconnects to the server (such as aftertemporarily losing its Internet connection), it will get a newconnection each time. The onConnection callbacks will be calledagain, and the new connection will have a new connection id.

In the future, when client reconnection is fully implemented,reconnecting from the client will reconnect to the same connection onthe server: the onConnection callback won’t be called for thatconnection again, and the connection will still have the sameconnection id.

Anywhere

DDP.connect(url)

import { DDP } from 'meteor/ddp-client' (ddp-client/common/namespace.js, line 63)

Connect to the server of a different Meteor application to subscribe to its document sets and invoke its remote methods.

Arguments

  • urlString
  • The URL of another Meteor application.

To call methods on another Meteor application or subscribe to its datasets, call DDP.connect with the URL of the application.DDP.connect returns an object which provides:

By default, clients open a connection to the server from which they’re loaded.When you call Meteor.subscribe, Meteor.status, Meteor.call, andMeteor.apply, you are using a connection back to that defaultserver.

Anywhere

DDP.onReconnect(callback)

import { DDP } from 'meteor/ddp-client' (ddp-client/common/namespace.js, line 80)

Register a function to call as the first step ofreconnecting. This function can call methods which will be executed beforeany other outstanding methods. For example, this can be used to re-establishthe appropriate authentication context on the connection.

Arguments

  • callbackFunction
  • The function to call. It will be called with asingle argument, the connection object that is reconnecting.