GithubHelp home page GithubHelp logo

meteor-ddp's Introduction

Meteor-DDP

A promise-based Meteor DDP client for version pre1 introduced in Meteor 0.5.7.

Dependencies

  • jQuery 1.5+ (Uses $.Deferred)

Methods

  • connect() - Starts a WebSocket connection and lets the server know we're about to talk some DDP. Returns -> Promise which resolves on succesful connection.
var ddp = new MeteorDdp('ws://yourApp.meteor.com/websocket');
ddp.connect().done(function() {
  console.log('Connected!');
});
  • call(methodName, [params, ...]) - Does a Remote Procedure Call on any method exposed through Meteor.methods on the server. Returns -> Promise which resolves with any returned data.
/* Lets say we can RPC a createPlayer method which returns a playerId. Lets also say 
   that we need the playerId in order to join a game (via a joinGame method 
   which returns a gameId). Here's a couple of ways to do this with promises: 
*/

// Using the done method...
var createPlayer = ddp.call('createPlayer');
createPlayer.done(function(playerId) {
  var joinGame = ddp.call('joinGame', [playerId]);
  joinGame.done(function(gameId) {
    console.log("We joined a game, here's the game id: ", gameId);
  });
});

// We can pipe it... (Note: pipe is deprecated as of jQuery 1.8)
var createPlayer = ddp.call('createPlayer');
var joinGame = createPlayer.pipe(function(playerId) {
  return ddp.call('joinGame', [playerId]);
});
joinGame.done(function(gameId) {
  console.log('We joined a game: ', gameId);
});

// We can use when...then...
$.when(ddp.call('createPlayer')).then(function(playerId) {
  ddp.call('joinGame', [playerId]).done(function(gameId) {
    console.log('We joined a game! Game id: ', gameId);
  });
});
  • subscribe(subscriptionName, [params, ...]) - Subscribes to data published on the server. You can observe changes on a collection by using the 'watch' method. Returns -> Promise which resolves on successful subscription and fails otherwise.
// Subscribing returns a promise which resolved on success, but 
// you probably only care if the subscription fails...
ddp.subscribe('plyers', [gameId]).fail(function(err) {
  console.log('We actually wanted to subscribe to players not plyers...');
});
  • unsubscribe(subscriptionName) - Unsubscribes to data published on the server. Leaves local collection intact. Returns -> Promise which resolves on successful unsubscription and fails if something went wrong in the process or subscription never existed.
var unsubPlayers = ddp.unsubscribe('players');
unsubPlayers.done(function() {
  console.log("Successfully unsubscribed to players");
});
unsubPlayers.fail(function(err) {
  console.log("Something went wrong, couldn't unsub players. ", err);
});
  • watch(collectionName, callback) - Observe a collection and be notified whenever that collection changes via your callback. A copy of the modified document will be sent as argument to the callback. Returns -> void
// So say we subscribed to the `players` collection and want to be notified when any change occurs:
ddp.watch('players', function(changedDoc, message) {
  console.log("The players collection changed. Here's what changed: ", changedDoc, message);

  // Was it removed?
  if (message === "removed") {
    console.log("This document doesn't exist in our collection anymore :(");
  }

});
  • getCollection(collectionName) - Returns -> An Object containing the locally stored collection.
ddp.getCollection('rooms'); // -> {id1: {document1}, id2: {document2}, ...}
  • getDocument(collectionName, documentId) - Returns -> The document with specified documentId belonging to collectionName.
ddp.getDocument('rooms', '4ec81e1b-2e16-42f4-a915-cc18ad7bdb0c') // -> {document}
  • close() - Closes the WebSocket connection. Returns -> void
ddp.close(); // yeah...

Oauth Methods

  • loginWithOauth(oauthLoginUrl) - Log into meteor with oauth. Returns -> Promise which resolves on login.
ddp.loginWithOauth(
    //setup the twitter oauth login url
    function (credentialToken) {
        var callbackUrl = "http://localhost:3000/_oauth/twitter?close&state=" + credentialToken;

        var loginUrl = "http://localhost:3000/_oauth/twitter/?requestTokenAndRedirect="
            + encodeURIComponent(callbackUrl)
            + "&state=" + credentialToken;

        return loginUrl;
    }
).then(function () {
     console.log("We are logged in.");
  });
  • logout() - Logout from meteor. Returns -> Promise which resolves on logout.
ddp.logout();
  • oauthPrompt(timeoutInSeconds) - Reopen the oauth prompt. This re-authorizes with your oauth provider, unlike loginWithOauth which just makes sure the user is authenticated with meteor. Returns -> Promise which resolves on login.
ddp.oauthPrompt();

meteor-ddp's People

Contributors

ef2k avatar jperl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

meteor-ddp's Issues

loginWithOauth example for Google

I can't see to get it working with Google. An example would be super helpful if you happened to have it around.

I'm trying

ddp.loginWithOauth(
    //setup the twitter oauth login url
    function (credentialToken) {
        var callbackUrl = "http://localhost:3000/_oauth/google?close&state=" + credentialToken;

        var loginUrl = "http://localhost:3000/_oauth/google/?requestTokenAndRedirect="
            + encodeURIComponent(callbackUrl)
            + "&state=" + credentialToken;

        return loginUrl;
    }
)

A window pops up for a second and disappears.

On the server side I see:

W20141126-22:30:07.104(-8) (oauth_server.js:78) Unable to parse state from OAuth query: -�o;avF·ªÚ
W20141126-22:30:07.117(-8) (oauth_server.js:78) Unable to parse state from OAuth query: -�o;avF·ªÚ
W20141126-22:30:07.121(-8) (oauth_server.js:78) Unable to parse state from OAuth query: -�o;avF·ªÚ
W20141126-22:30:07.122(-8) (oauth_server.js:398) Error in OAuth Server: Failed to complete OAuth handshake with Google. failed [400] {   "error" : "invalid_grant",   "error_description" : "Code was already redeemed." }
I20141126-22:30:07.531(-8)? Exception while invoking method 'login' Error: Match error: Missing key 'credentialSecret'
I20141126-22:30:07.531(-8)?     at packages/check/match.js:308
I20141126-22:30:07.532(-8)?     at Function._.each._.forEach (packages/underscore/underscore.js:113)
I20141126-22:30:07.532(-8)?     at checkSubtree (packages/check/match.js:307)
I20141126-22:30:07.532(-8)?     at check (packages/check/match.js:32)
I20141126-22:30:07.533(-8)?     at Package (packages/accounts-oauth/oauth_server.js:7)
I20141126-22:30:07.533(-8)?     at packages/accounts-base/accounts_server.js:383
I20141126-22:30:07.533(-8)?     at tryLoginMethod (packages/accounts-base/accounts_server.js:186)
I20141126-22:30:07.534(-8)?     at runLoginHandlers (packages/accounts-base/accounts_server.js:380)

How can the server refuse the connection?

First of all, awesome script! I've made a version that replaces jquery Deferred dependency. If you're interested I can fork the repro.

But, how to deal with following use case:

What if the client can only connect to the server if it passes a specific token in the connect() call. How could I make that setup? Any suggestions?

ddp callbacks for call() not working

var addCheck = ddp.call('addCheckRoom', [url]);
  addCheck.done(function(){
    callback();
  });

This is what I'm trying to do. The method 'addCheckRoom' works as expected but the callback is not firing.

Console logs

Could we remove them, or making them configurable? Also, great package.

Feature request: remove DOM dependencies

As the title says,

specifically jQuery dependency.
Otherwise it doesn't work with frameworks like Nativescript which support NPM modules but only if they are without DOM dependencies

License

Could you please specify under which license this project is published?
Would like to integrate the code into another project but need to be clear on the license.
Thank you in advance!

Issue with _changedoc function

When you receive a 'changed' event, get an error stating,
Uncaught TypeError: Cannot read property 'id' of undefined
at MeteorDdp._changeDoc (meteor-ddp.js:114)
at WebSocket.self.sock.onmessage (meteor-ddp.js:55)
MeteorDdp._changeDoc @ meteor-ddp.js:114
self.sock.onmessage @ meteor-ddp.js:55

This error comes on subscription.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.