Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

simpleddp

Gregivy4kMIT2.2.4TypeScript support: definitely-typed

The aim of this library is to simplify the process of working with meteor server over DDP protocol using external JS environments

ddp, simpleddp, simple, ddpjs, ddp.js, DDP, WebSocket, client, meteor

readme

npm version Build Status Dependency Status devDependency Status

SimpleDDP 🥚

The aim of this library is to simplify the process of working with Meteor.js server over DDP protocol using external JS environments (like Node.js, Cordova, Ionic, ReactNative, etc).

It is battle tested 🏰 in production and ready to use 🔨.

If you like this project ⭐ is always welcome.

Important

SimpleDDP is written in ES6 and uses modern features like promises. Though its precompiled with Babel, your js environment must support ES6 features. So if you are planning to use SimpleDDP be sure that your js environment supports ES6 features or include polyfills yourself (like Babel Polyfill).

Project uses semantic versioning 2.0.0.

DDP (protocol) specification.

CHANGE LOG

Install

npm install simpleddp --save

Documentation

Plugins

Adding custom EJSON types

Example

First of all you need WebSocket implementation for your node app. We will use isomorphic-ws package for this since it works on the client and serverside.

npm install isomorphic-ws ws --save

Import/require simpleDDP.

const simpleDDP = require("simpleddp"); // nodejs
const ws = require("isomorphic-ws");

or

import simpleDDP from 'simpleDDP'; // ES6
import ws from 'isomorphic-ws';

Now you should make a new simpleDDP instance.

let opts = {
    endpoint: "ws://someserver.com/websocket",
    SocketConstructor: ws,
    reconnectInterval: 5000
};
const server = new simpleDDP(opts);

Connection is not going to be established immediately after you create a simpleDDP instance. If you need to check your connection simply use server.connected property which is true if you are connected to the server, otherwise it's false.

You can also add some events for connection status.

server.on('connected', () => {
    // do something
});

server.on('disconnected', () => {
    // for example show alert to user
});

server.on('error', (e) => {
    // global errors from server
});

As an alternative you can use a async/await style (or then(...)).

(async ()=>{
  await server.connect();
  // connection is ready here
})();

The next thing we are going to do is subscribing to some publications.

let userSub = server.subscribe("user_pub");
let otherSub = server.subscribe("other_pub",'param1',2); // you can specify arguments for subscription

(async ()=>{
  await userSub.ready();
  let nextSub = server.subscribe("next_pub"); // subscribing after userSub is ready
  await nextSub.ready();
  //all subs are ready here
})();

You can fetch all things you've subscribed for using server.collection method. Also you can get reactive data sources (plain js objects which will be automatically updated if something changes on the server).

(async ()=>{

  // call some method
  await server.call('somemethod');

  let userSub = server.subscribe("user",userId);
  await userSub.ready();

  // get non-reactive user object
  let user = server.collection('users').filter(user=>user.id==userId).fetch()[0];

  // get reactive user object
  let userReactiveCursor = server.collection('users').filter(user=>user.id==userId).reactive().one();
  let userReactiveObject = userReactiveCursor.data();

  // observing the changes
  server.collection('users').filter(user=>user.id==userId).onChange(({prev,next})=>{
    console.log('previus user data',state.prev);
    console.log('next user data',state.next);
  });

  // observing changes in reactive data source
  userReactiveCursor.onChange((newData)=>{
    console.log('new user state', newData);
  });

  let participantsSub = server.subscribe("participants");

  await participantsSub.ready();

  let reactiveCollection = server.collection('participants').reactive();

  // reactive reduce
  let reducedReactive = reactiveCollection.reduce((acc,val,i,arr)=>{
    if (i<arr.length-1)  {
      return acc + val.age;
    } else {
      return (acc + val.age)/arr.length;
    }
  },0);

  // reactive mean age of all participants
  let meanAge = reducedReactive.data();

  // observing changes in reactive data source
  userReactiveCursor.onChange((newData)=>{
    console.log('new user state', newData);
  });
})();

changelog

CHANGELOG

2.2.4

  • Fixed bug with auto re-subscribing when clearDataOnReconnection=true (default). Pseudo removing messages arrived later than the first subscription. It was causing possible data loss.
  • Fixex bug with resolving clearData().
  • Updated simpleddp-core package.
  • Small changes in plugin system, added event clientReady.

2.2.3

  • Fixed bug with ddpSubscription.restart and ddpSubscription.nosub when error comes from the server.

2.2.2

  • Fixed bug with maxTimeout.

2.2.1

  • Fixed bug with ddpReactiveCollection sorting. In some cases data array didn't recieve valid updates.

2.2.0

  • restartSubsOnConnect method renamed to restartSubs.
  • Added property clearDataOnReconnection to simpleDDP class constructor.
  • Docs improvments.

2.1.1

  • Fixed bug with ddpSubscription restart (loosing arguments).
  • Fixed rare situation with ddp message removed arriving before any other.
  • API fix.

2.1.0

  • Fixed dependencies vulnerabilities.
  • Added documentation for custom EJSON types.
  • Added maxTimeout to support the maximum wait for a response from the server to the method call.

2.0.2

  • Fixed dependencies vulnerabilities.

2.0.1

  • Fix. If change message arrives and no collection is found simpleddp acts like it is an added message.

2.0.0

  • Added semantic versioning.
  • call renamed to apply.
  • New call works like apply but accepts parameters for server method as a list of function arguments.
  • subid property of the subscription object renamed to subscriptionId.
  • Added subscribe method. Works like sub but accepts parameters for server publication as a list of function arguments.

1.2.3

  • Updated simpleddp-core package.

1.2.2

  • Fixed bug with EJSON types.

1.2.1

  • Updated simpleddp-core package.
  • Added support for putting method call at the beginning of the requests queue.