npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

meteor-sdk

v0.0.21

Published

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

Downloads

86

Readme

npm version Build Status Dependency Status devDependency Status

SimpleDDP 🥚

Please note

This is a reimplementation of DDPClient using TypeScript and other modern tools.

Under the hood is the DDPClient that everyone knows and loves, but with a new look and feel. This is a work in progress, so please be patient with me as I work out the kinks.


What this fork has to offer new?

Typescript safety, and a more modern codebase. I rewrote the codebase using TypeScript, and added a few TS feature like Generics to collections. This means that you can now use the collection methods with a type definition, and get type safety when using the collection methods. and calling methods in a safer way for collection

  const sub = server.subscribe('tasksByUser');
await sub.ready()
server
  .collection<Task>('tasks')
  .filter((t) => t) // t has the type of Tasks
  .onChange(({ prev, next, predicatePassed }) => { // prev and next have the type of Tasks
    if (predicatePassed) {
      // do something
    }
  });
const t = server.collection<Task>('tasks').fetch() // t has the type of Tasks[]

for methods


const tasks = await server.call<[], Task[]>('fetchTasks') // Task[] is the return type
// the first argument is the arguments to the method, and the second is the return type

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 meteor-sdk --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 DDPClient.

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

or

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

Now you should make a new DDPClient instance.

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

Connection is not going to be established immediately after you create a DDPClient 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');

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

  // get non-reactive user object
  const user = server.collection('users').filter(newObjFullCopy, i - 1, this.collections[m.collection]).fetch()[0];

  // get reactive user object
  const userReactiveCursor = server.collection('users').filter(newObjFullCopy, i - 1, this.collections[m.collection]).reactive().one();
  const userReactiveObject = userReactiveCursor.data();

  // observing the changes
  server.collection('users').filter(newObjFullCopy, i - 1, this.collections[m.collection]).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);
  });

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

  await participantsSub.ready();

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

  // reactive reduce
  const 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
  const meanAge = reducedReactive.data();

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