UNPKG

meteor-sdk

Version:

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

534 lines (356 loc) 22.5 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>SimpleDDP Docs</title> <meta name="description" content="SimpleDDP Docs" /> <meta name="keywords" content="ddp, simpleddp, websocket, meteor" /> <meta name="keyword" content="ddp, simpleddp, websocket, meteor" /> <meta property="og:title" content="SimpleDDP Docs"/> <meta property="og:type" content="website"/> <meta property="og:image" content="https://github.com/Gregivy/simpleddp/raw/2.x.x/simpleddp.png"/> <meta property="og:site_name" content="SimpleDDP Docs"/> <meta property="og:url" content=""/> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="scripts/prettify/prettify.js"></script> <script src="scripts/prettify/lang-css.js"></script> <script src="scripts/jquery.min.js"></script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link href="https://fonts.googleapis.com/css?family=Libre+Franklin:400,700" rel="stylesheet"> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css"> <link type="text/css" rel="stylesheet" href="styles/main.css"> <script> var config = {"monospaceLinks":false,"cleverLinks":false,"applicationName":"SimpleDDP","disqus":"","googleAnalytics":"","openGraph":{"title":"SimpleDDP Docs","type":"website","image":"https://github.com/Gregivy/simpleddp/raw/2.x.x/simpleddp.png","site_name":"SimpleDDP Docs","url":""},"meta":{"title":"SimpleDDP Docs","description":"SimpleDDP Docs","keyword":"ddp, simpleddp, websocket, meteor"},"linenums":true,"default":{}}; </script> </head> <body> <div id="wrap" class="clearfix"> <div class="navigation"> <h3 class="applicationName"><a href="index.html">SimpleDDP</a></h3> <button id="menuToggle" class="btn btn-link btn-lg menu-toggle"> <span class="glyphicon glyphicon-menu-hamburger"></span> </button> <div class="search"> <input id="search" type="text" class="form-control input-md" placeholder="Search..."> </div> <ul class="list"> <li class="item" data-name="ddpCollection"> <span class="title "> <a href="ddpCollection.html">ddpCollection</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpCollection#exportData"><a href="ddpCollection.html#exportData">exportData</a></li> <li class="parent " data-name="ddpCollection#fetch"><a href="ddpCollection.html#fetch">fetch</a></li> <li class="parent " data-name="ddpCollection#filter"><a href="ddpCollection.html#filter">filter</a></li> <li class="parent " data-name="ddpCollection#importData"><a href="ddpCollection.html#importData">importData</a></li> <li class="parent " data-name="ddpCollection#onChange"><a href="ddpCollection.html#onChange">onChange</a></li> <li class="parent " data-name="ddpCollection#reactive"><a href="ddpCollection.html#reactive">reactive</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="ddpEventListener"> <span class="title "> <a href="ddpEventListener.html">ddpEventListener</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpEventListener#start"><a href="ddpEventListener.html#start">start</a></li> <li class="parent " data-name="ddpEventListener#stop"><a href="ddpEventListener.html#stop">stop</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="ddpOnChange"> <span class="title "> <a href="ddpOnChange.html">ddpOnChange</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpOnChange#start"><a href="ddpOnChange.html#start">start</a></li> <li class="parent " data-name="ddpOnChange#stop"><a href="ddpOnChange.html#stop">stop</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="ddpReactiveCollection"> <span class="title "> <a href="ddpReactiveCollection.html">ddpReactiveCollection</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpReactiveCollection#_updateReactiveObjects"><a href="ddpReactiveCollection.html#_updateReactiveObjects">_updateReactiveObjects</a></li> <li class="parent " data-name="ddpReactiveCollection#count"><a href="ddpReactiveCollection.html#count">count</a></li> <li class="parent " data-name="ddpReactiveCollection#data"><a href="ddpReactiveCollection.html#data">data</a></li> <li class="parent " data-name="ddpReactiveCollection#map"><a href="ddpReactiveCollection.html#map">map</a></li> <li class="parent " data-name="ddpReactiveCollection#onChange"><a href="ddpReactiveCollection.html#onChange">onChange</a></li> <li class="parent " data-name="ddpReactiveCollection#one"><a href="ddpReactiveCollection.html#one">one</a></li> <li class="parent " data-name="ddpReactiveCollection#reduce"><a href="ddpReactiveCollection.html#reduce">reduce</a></li> <li class="parent " data-name="ddpReactiveCollection#settings"><a href="ddpReactiveCollection.html#settings">settings</a></li> <li class="parent " data-name="ddpReactiveCollection#sort"><a href="ddpReactiveCollection.html#sort">sort</a></li> <li class="parent " data-name="ddpReactiveCollection#start"><a href="ddpReactiveCollection.html#start">start</a></li> <li class="parent " data-name="ddpReactiveCollection#stop"><a href="ddpReactiveCollection.html#stop">stop</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="ddpReactiveDocument"> <span class="title "> <a href="ddpReactiveDocument.html">ddpReactiveDocument</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpReactiveDocument#data"><a href="ddpReactiveDocument.html#data">data</a></li> <li class="parent " data-name="ddpReactiveDocument#onChange"><a href="ddpReactiveDocument.html#onChange">onChange</a></li> <li class="parent " data-name="ddpReactiveDocument#settings"><a href="ddpReactiveDocument.html#settings">settings</a></li> <li class="parent " data-name="ddpReactiveDocument#start"><a href="ddpReactiveDocument.html#start">start</a></li> <li class="parent " data-name="ddpReactiveDocument#stop"><a href="ddpReactiveDocument.html#stop">stop</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="ddpReducer"> <span class="title "> <a href="ddpReducer.html">ddpReducer</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpReducer#data"><a href="ddpReducer.html#data">data</a></li> <li class="parent " data-name="ddpReducer#doReduce"><a href="ddpReducer.html#doReduce">doReduce</a></li> <li class="parent " data-name="ddpReducer#onChange"><a href="ddpReducer.html#onChange">onChange</a></li> <li class="parent " data-name="ddpReducer#start"><a href="ddpReducer.html#start">start</a></li> <li class="parent " data-name="ddpReducer#stop"><a href="ddpReducer.html#stop">stop</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="ddpSubscription"> <span class="title "> <a href="ddpSubscription.html">ddpSubscription</a> </span> <ul class="members itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="ddpSubscription#isOn"><a href="ddpSubscription.html#isOn">isOn</a></li> <li class="parent " data-name="ddpSubscription#isReady"><a href="ddpSubscription.html#isReady">isReady</a></li> <li class="parent " data-name="ddpSubscription#isStopped"><a href="ddpSubscription.html#isStopped">isStopped</a></li> <li class="parent " data-name="ddpSubscription#nosub"><a href="ddpSubscription.html#nosub">nosub</a></li> <li class="parent " data-name="ddpSubscription#onNosub"><a href="ddpSubscription.html#onNosub">onNosub</a></li> <li class="parent " data-name="ddpSubscription#onReady"><a href="ddpSubscription.html#onReady">onReady</a></li> <li class="parent " data-name="ddpSubscription#ready"><a href="ddpSubscription.html#ready">ready</a></li> <li class="parent " data-name="ddpSubscription#remove"><a href="ddpSubscription.html#remove">remove</a></li> <li class="parent " data-name="ddpSubscription#restart"><a href="ddpSubscription.html#restart">restart</a></li> <li class="parent " data-name="ddpSubscription#start"><a href="ddpSubscription.html#start">start</a></li> <li class="parent " data-name="ddpSubscription#stop"><a href="ddpSubscription.html#stop">stop</a></li> </ul> <ul class="events itemMembers"> </ul> </li> <li class="item" data-name="DDPClient"> <span class="title "> <a href="simpleDDP.html">DDPClient</a> </span> <ul class="members itemMembers"> <span class="subtitle">Members</span> <li class="parent " data-name="DDPClient#collections"><a href="simpleDDP.html#collections">collections</a></li> <li class="parent " data-name="DDPClient#connected"><a href="simpleDDP.html#connected">connected</a></li> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="typedefs itemMembers"> </ul> <ul class="methods itemMembers"> <span class="subtitle">Methods</span> <li class="parent " data-name="DDPClient#apply"><a href="simpleDDP.html#apply">apply</a></li> <li class="parent " data-name="DDPClient#call"><a href="simpleDDP.html#call">call</a></li> <li class="parent " data-name="DDPClient#clearData"><a href="simpleDDP.html#clearData">clearData</a></li> <li class="parent " data-name="DDPClient#collection"><a href="simpleDDP.html#collection">collection</a></li> <li class="parent " data-name="DDPClient#connect"><a href="simpleDDP.html#connect">connect</a></li> <li class="parent " data-name="DDPClient#disconnect"><a href="simpleDDP.html#disconnect">disconnect</a></li> <li class="parent " data-name="DDPClient#exportData"><a href="simpleDDP.html#exportData">exportData</a></li> <li class="parent " data-name="DDPClient#importData"><a href="simpleDDP.html#importData">importData</a></li> <li class="parent " data-name="DDPClient#markAsReady"><a href="simpleDDP.html#markAsReady">markAsReady</a></li> <li class="parent " data-name="DDPClient#on"><a href="simpleDDP.html#on">on</a></li> <li class="parent " data-name="DDPClient#stopChangeListeners"><a href="simpleDDP.html#stopChangeListeners">stopChangeListeners</a></li> <li class="parent " data-name="DDPClient#sub"><a href="simpleDDP.html#sub">sub</a></li> <li class="parent " data-name="DDPClient#subscribe"><a href="simpleDDP.html#subscribe">subscribe</a></li> </ul> <ul class="events itemMembers"> </ul> </li> </ul> </div> <div class="main"> <h1 class="page-title" data-filename="index.html">Index</h1> <h3> </h3> <section> <article class="readme content-size"><p><a target="_blank" href="https://badge.fury.io/js/simpleddp"><img src="https://badge.fury.io/js/simpleddp.svg" alt="npm version"></a> <a target="_blank" href="https://travis-ci.org/Gregivy/simpleddp"><img src="https://travis-ci.org/Gregivy/simpleddp.svg?branch=master" alt="Build Status"></a> <a target="_blank" href="https://david-dm.org/gregivy/simpleddp"><img src="https://david-dm.org/gregivy/simpleddp.svg" alt="Dependency Status"></a> <a target="_blank" href="https://david-dm.org/gregivy/simpleddp#info=devDependencies"><img src="https://david-dm.org/gregivy/simpleddp/dev-status.svg" alt="devDependency Status"></a></p> <p align="center"> <img width="300" height="300" src="https://github.com/Gregivy/simpleddp/raw/master/simpleddp.png"> </p> <h1>SimpleDDP 🥚</h1> <p>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).</p> <p>It is battle tested 🏰 in production and ready to use 🔨.</p> <p>If you like this project ⭐ is always welcome.</p> <p><strong>Important</strong></p> <p>SimpleDDP is written in ES6 and uses modern features like <em>promises</em>. 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).</p> <p>Project uses <a target="_blank" href="https://semver.org/spec/v2.0.0.html">semantic versioning 2.0.0</a>.</p> <p>DDP (protocol) <a target="_blank" href="https://github.com/meteor/meteor/blob/devel/packages/ddp/DDP.md">specification</a>.</p> <h2><a target="_blank" href="https://github.com/Gregivy/simpleddp/blob/master/CHANGELOG.md">CHANGE LOG</a></h2> <h2>Install</h2> <p><code>npm install simpleddp --save</code></p> <h2><a target="_blank" href="https://gregivy.github.io/simpleddp/simpleDDP.html">Documentation</a></h2> <h2>Plugins</h2> <ul> <li><a target="_blank" href="https://github.com/Gregivy/simpleddp-plugin-login">simpleddp-plugin-login</a></li> </ul> <h2><a target="_blank" href="https://github.com/Gregivy/simpleddp/blob/master/custom_ejson.md">Adding custom EJSON types</a></h2> <h2>Example</h2> <p>First of all you need WebSocket implementation for your node app. We will use <a target="_blank" href="https://www.npmjs.com/package/isomorphic-ws">isomorphic-ws</a> package for this since it works on the client and serverside.</p> <p><code>npm install isomorphic-ws ws --save</code></p> <p>Import/require <code>DDPClient</code>.</p> <pre class="prettyprint source lang-javascript"><code>const DDPClient = require(&quot;simpleddp&quot;); // nodejs const ws = require(&quot;isomorphic-ws&quot;); </code></pre> <p>or</p> <pre class="prettyprint source lang-javascript"><code>import DDPClient from 'DDPClient'; // ES6 import ws from 'isomorphic-ws'; </code></pre> <p>Now you should make a new DDPClient instance.</p> <pre class="prettyprint source lang-javascript"><code>let opts = { endpoint: &quot;ws://someserver.com/websocket&quot;, SocketConstructor: ws, reconnectInterval: 5000 }; const server = new DDPClient(opts); </code></pre> <p>Connection is not going to be established immediately after you create a DDPClient instance. If you need to check your connection simply use <code>server.connected</code> property which is <code>true</code> if you are connected to the server, otherwise it's <code>false</code>.</p> <p>You can also add some events for connection status.</p> <pre class="prettyprint source lang-javascript"><code>server.on('connected', () => { // do something }); server.on('disconnected', () => { // for example show alert to user }); server.on('error', (e) => { // global errors from server }); </code></pre> <p>As an alternative you can use a <em>async/await</em> style (or <code>then(...)</code>).</p> <pre class="prettyprint source lang-javascript"><code>(async ()=>{ await server.connect(); // connection is ready here })(); </code></pre> <p>The next thing we are going to do is subscribing to some publications.</p> <pre class="prettyprint source lang-javascript"><code>let userSub = server.subscribe(&quot;user_pub&quot;); let otherSub = server.subscribe(&quot;other_pub&quot;,'param1',2); // you can specify arguments for subscription (async ()=>{ await userSub.ready(); let nextSub = server.subscribe(&quot;next_pub&quot;); // subscribing after userSub is ready await nextSub.ready(); //all subs are ready here })(); </code></pre> <p>You can fetch all things you've subscribed for using <a target="_blank" href="https://gregivy.github.io/simpleddp/simpleDDP.html#collection">server.collection</a> method. Also you can get reactive data sources (plain js objects which will be automatically updated if something changes on the server).</p> <pre class="prettyprint source lang-javascript"><code>(async ()=>{ // call some method await server.call('somemethod'); let userSub = server.subscribe(&quot;user&quot;,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(&quot;participants&quot;); await participantsSub.ready(); let reactiveCollection = server.collection('participants').reactive(); // reactive reduce let reducedReactive = reactiveCollection.reduce((acc,val,i,arr)=>{ if (i&lt;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); }); })(); </code></pre></article> </section> <footer class="content-size"> <div class="footer"> Documentation generated by <a target="_blank" href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.3</a> on Wed Aug 07 2019 02:35:33 GMT+0300 (MSK) </div> </footer> </div> </div> <script>prettyPrint();</script> <script src="scripts/main.js"></script> </body> </html>