Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

An easy to use DDP client library

License

NotificationsYou must be signed in to change notification settings

Gregivy/simpleddp

Repository files navigation

npm versionBuild StatusDependency StatusdevDependency 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 likepromises. 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 usessemantic versioning 2.0.0.

DDP (protocol)specification.

Install

npm install simpleddp --save

Plugins

Example

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

npm install isomorphic-ws ws --save

Import/requiresimpleDDP.

constsimpleDDP=require("simpleddp");// nodejsconstws=require("isomorphic-ws");

or

importsimpleDDPfrom'simpleDDP';// ES6importwsfrom'isomorphic-ws';

Now you should make a new simpleDDP instance.

letopts={endpoint:"ws://someserver.com/websocket",SocketConstructor:ws,reconnectInterval:5000};constserver=newsimpleDDP(opts);

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

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 aasync/await style (orthen(...)).

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

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

letuserSub=server.subscribe("user_pub");letotherSub=server.subscribe("other_pub",'param1',2);// you can specify arguments for subscription(async()=>{awaituserSub.ready();letnextSub=server.subscribe("next_pub");// subscribing after userSub is readyawaitnextSub.ready();//all subs are ready here})();

You can fetch all things you've subscribed for usingserver.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 methodawaitserver.call('somemethod');letuserSub=server.subscribe("user",userId);awaituserSub.ready();// get non-reactive user objectletuser=server.collection('users').filter(user=>user.id==userId).fetch()[0];// get reactive user objectletuserReactiveCursor=server.collection('users').filter(user=>user.id==userId).reactive().one();letuserReactiveObject=userReactiveCursor.data();// observing the changesserver.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 sourceuserReactiveCursor.onChange((newData)=>{console.log('new user state',newData);});letparticipantsSub=server.subscribe("participants");awaitparticipantsSub.ready();letreactiveCollection=server.collection('participants').reactive();// reactive reduceletreducedReactive=reactiveCollection.reduce((acc,val,i,arr)=>{if(i<arr.length-1){returnacc+val.age;}else{return(acc+val.age)/arr.length;}},0);// reactive mean age of all participantsletmeanAge=reducedReactive.data();// observing changes in reactive data sourceuserReactiveCursor.onChange((newData)=>{console.log('new user state',newData);});})();

[8]ページ先頭

©2009-2025 Movatter.jp