Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ddp.js

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ddp.js

ddp javascript client

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
4.9K
decreased by-22.27%
Maintainers
1
Weekly downloads
 
Created
Source

Build Status Coverage Status Dependency Status devDependency Status

#WARNING Breaking changes from 0.6.x to 1.0.0, read the CHANGELOG for more info.

#ddp.js

A javascript isomorphic ddp client.

##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

Via npm

npm install ddp.js

Or via bower

bower install ddp.js

##Example usage

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

ddp.on("connected", function () {
    console.log("Connected");

    var subId = ddp.sub("myCollection");
    ddp.on("ready", function (message) {
        if (message.id === subId) {
            console.log("Subscruption to myCollection ready");
        }
    });
    ddp.on("added", function (message) {
        console.log(message.collection);
    });

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

##Tests

npm test to run tests, npm run coverage to generate the coverage report.

##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).

####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: 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.


###DDP.sub(name, params)

Subscribes to a server publication.

####Arguments

  • name string required: name of the server publication.

  • params array required: 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.


###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).

##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

Keywords

FAQs

Package last updated on 05 Jul 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc