Class: simpleDDP

simpleDDP

Creates an instance of simpleDDP class. After being constructed, the instance will establish a connection with the DDP server and will try to maintain it open.

new simpleDDP (options, plugins)simpleDDP

Name Type Description
options Object
Name Type Default Description
endpoint string

The location of the websocket server. Its format depends on the type of socket you are using. If you are using https connection you have to use wss:// protocol.

SocketConstructor function

The constructor function that will be used to construct the socket. Meteor (currently the only DDP server available) supports websockets and SockJS sockets. So, practically speaking, this means that on the browser you can use either the browser's native WebSocket constructor or the SockJS constructor provided by the SockJS library. On the server you can use whichever library implements the websocket protocol (e.g. faye-websocket).

autoConnect boolean true optional

Whether to establish the connection to the server upon instantiation. When false, one can manually establish the connection with the connect method.

autoReconnect boolean true optional

Whether to try to reconnect to the server when the socket connection closes, unless the closing was initiated by a call to the disconnect method.

reconnectInterval number 1000 optional

The interval in ms between reconnection attempts.

clearDataOnReconnection boolean true optional

Whether to clear all collections data after a reconnection. This invokes fake removed messages on every document.

maxTimeout number optional

Maximum wait for a response from the server to the method call. Default no maxTimeout.

plugins Array optional

Array of plugins.

Version:
  • 2.2.4
Returns:
Type Description
simpleDDP
  • A new simpleDDP instance.
Example
var opts = {
   endpoint: "ws://someserver.com/websocket",
   SocketConstructor: WebSocket,
   reconnectInterval: 5000
};
var server = new simpleDDP(opts);

Members

collections Object

All collections data recieved from server.

connected Boolean

Whether the client is connected to server.

Methods

apply (method, arguments, atBeginning)Promise

Calls a remote method with arguments passed in array.

Name Type Default Description
method string

Name of the server publication.

arguments Array optional

Array of parameters to pass to the remote method. Pass an empty array or don't pass anything if you do not wish to pass any parameters.

atBeginning boolean false optional

If true puts method call at the beginning of the requests queue.

Returns:
Type Description
Promise
  • Promise object, which resolves when receives a result send by server and rejects when receives an error send by server.
Example
server.apply("method1").then(function(result) {
	console.log(result); //show result message in console
   if (result.someId) {
       //server sends us someId, lets call next method using this id
       return server.apply("method2",[result.someId]);
   } else {
       //we didn't recieve an id, lets throw an error
       throw "no id sent";
   }
}).then(function(result) {
   console.log(result); //show result message from second method
}).catch(function(error) {
   console.log(result); //show error message in console
});

call (method, args)Promise

Calls a remote method with arguments passed after the first argument. Syntactic sugar for @see apply.

Name Type Description
method string

Name of the server publication.

args any optional repeatable

List of parameters to pass to the remote method. Parameters are passed as function arguments.

Returns:
Type Description
Promise
  • Promise object, which resolves when receives a result send by server and rejects when receives an error send by server.

clearData ()Promise

Removes all documents like if it was removed by the server publication.

Returns:
Type Description
Promise
  • Resolves when data is successfully removed.

collection (name)ddpCollection

Use this for fetching the subscribed data and for reactivity inside the collection.

Name Type Description
name string

Collection name.

Returns:
Type Description
ddpCollection

connect ()Promise

Connects to the ddp server. The method is called automatically by the class constructor if the autoConnect option is set to true (default behavior).

Returns:
Type Description
Promise
  • Promise which resolves when connection is established.

disconnect ()Promise

Disconnects from the ddp server by closing the WebSocket connection. You can listen on the disconnected event to be notified of the disconnection.

Returns:
Type Description
Promise
  • Promise which resolves when connection is closed.

exportData (format)Object | string

Exports the data

Name Type Default Description
format string 'string' optional

Possible values are 'string' (EJSON string) and 'raw' (EJSON).

Returns:
Type Description
Object | string
  • EJSON string or EJSON.

importData (data)Promise

Imports the data like if it was published by the server.

Name Type Description
data Object | string

ESJON string or EJSON.

Returns:
Type Description
Promise
  • Resolves when data is successfully imported.

markAsReady (subs)Promise

Marks every passed @see ddpSubscription object as ready like if it was done by the server publication.

Name Type Description
subs Array

Array of @see ddpSubscription objects.

Returns:
Type Description
Promise
  • Resolves when all passed subscriptions are marked as ready.

on (event, f)ddpEventListener

Starts listening server for basic DDP event running f each time the message arrives.

Name Type Description
event string

Any event name from DDP specification. Default suppoted events: connected, disconnected, added, changed, removed, ready, nosub, error, ping, pong.

f function

Function which receives a message from a DDP server as a first argument each time server is invoking event.

Returns:
Type Description
ddpEventListener
Example
server.on('connected', () => {
    // you can show a success message here
});

server.on('disconnected', () => {
    // you can show a reconnection message here
});

stopChangeListeners ()

Stops all reactivity.

sub (pubname, arguments)ddpSubscription

Tries to subscribe to a specific publication on server.

Name Type Description
pubname string

Name of the publication on server.

arguments Array optional

Array of parameters to pass to the remote method. Pass an empty array or don't pass anything if you do not wish to pass any parameters.

Returns:
Type Description
ddpSubscription
  • Subscription.

subscribe (pubname, args)ddpSubscription

Tries to subscribe to a specific publication on server. Syntactic sugar for @see sub.

Name Type Description
pubname string

Name of the publication on server.

args any optional repeatable

List of parameters to pass to the remote method. Parameters are passed as function arguments.

Returns:
Type Description
ddpSubscription
  • Subscription.