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

@nqminds/ddp.js

v2.2.5

Published

ddp javascript client

Downloads

15

Readme

npm version Build Status Coverage Status Dependency Status devDependency Status

ddp.js

A javascript isomorphic/universal ddp client.

Warning

ddp.js@^2.0.0 is only distributed as an npm module instead of an UMD bundle. Also, bower has been removed as a method of distribution. If you need an UMD bundle or bower support, I'm open for suggestions to add back those methods of distribution without polluting this repo.

What is it for?

The purpose of this library is:

  • to set up and maintain a ddp connection with a ddp server, freeing the developer from having to do it on their own
  • to give the developer a clear, consistent API to communicate with the ddp server

Install

npm install ddp.js

Example usage

const DDP = require("ddp.js");
const options = {
    endpoint: "ws://localhost:3000/websocket",
    SocketConstructor: WebSocket
};
const ddp = new DDP(options);

ddp.on("connected", () => {
    console.log("Connected");
});

const subId = ddp.sub("mySubscription");
ddp.on("ready", message => {
    if (message.subs.includes(subId)) {
        console.log("mySubscription ready");
    }
});
ddp.on("added", message => {
    console.log(message.collection);
});

const myLoginParams = {
    user: {
        email: "[email protected]"
    },
    password: "hunter2"
};
const methodId = ddp.method("login", [myLoginParams]);
ddp.on("result", message => {
    if (message.id === methodId && !message.error) {
        console.log("Logged in!");
    }
});

Developing

After cloning the repository, install npm dependencies with npm install. Run npm test to run unit tests, or npm run dev to have mocha re-run your tests when source or test files change.

To run e2e tests, first install meteor. Then, start the meteor server with npm run start-meteor. Finally, run npm run e2e-test to run the e2e test suite, or npm run e2e-dev to have mocha re-run the suite when source or test files change.

Public API

new DDP(options)

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

Arguments

  • options object required

Available options are:

  • endpoint string required: the location of the websocket server. Its format depends on the type of socket you are using.

  • SocketConstructor function required: 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 optional [default: true]: whether to establish the connection to the server upon instantiation. When false, one can manually establish the connection with the connect method.

  • autoReconnect boolean optional [default: true]: 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 optional [default: 10000]: the interval in ms between reconnection attempts.

Returns

A new DDP instance, which is also an EventEmitter instance.


DDP.method(name, params)

Calls a remote method.

Arguments

  • name string required: name of the method to call.

  • params array required: array of parameters to pass to the remote method. Pass an empty array if you do not wish to pass any parameters.

Returns

The unique id (string) corresponding to the method call.

Example usage

Server code:

Meteor.methods({
    myMethod (param_0, param_1, param_2) {
        /* ... */
    }
});

Client code:

const methodCallId = ddp.method("myMethod", [param_0, param_1, param_2]);

DDP.sub(name, params)

Subscribes to a server publication.

Arguments

  • name string required: name of the server publication.

  • params array required: array of parameters to pass to the server publish function. Pass an empty array if you do not wish to pass any parameters.

Returns

The unique id (string) corresponding to the subscription call.

Example usage

Server code:

Meteor.publish("myPublication", (param_0, param_1, param_2) {
    /* ... */
});

Client code:

const subscriptionId = ddp.sub("myPublication", [param_0, param_1, param_2]);

DDP.unsub(id)

Unsubscribes to a previously-subscribed server publication.

Arguments

  • id string required: id of the subscription.

Returns

The id corresponding to the subscription call (not of much use, but I return it for consistency).


DDP.connect()

Connects to the ddp server. The method is called automatically by the class constructor if the autoConnect option is set to true (default behaviour). So there generally should be no need for the developer to call the method themselves.

Arguments

None

Returns

None


DDP.disconnect()

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

Arguments

None

Returns

None

Public events

Connection events

  • connected: emitted with no arguments when the DDP connection is established.

  • disconnected: emitted with no arguments when the DDP connection drops.

Subscription events

All the following events are emitted with one argument, the parsed DDP message. Further details can be found on the DDP spec page.

  • ready
  • nosub
  • added
  • changed
  • removed

Method events

All the following events are emitted with one argument, the parsed DDP message. Further details can be found on the DDP spec page.

  • result
  • updated