在浏览器中连接到Meteor DDP独立版?

时间:2016-03-24 12:17:13

标签: javascript meteor ddp

是否可以在浏览器中通过DDP订阅Meteor中的数据?

我找到了流星DDP package,声明可以这样做。

如何在浏览器中单独使用它来接收例如收藏更新?

或者你知道浏览器的其他工作DDP客户端吗?

2 个答案:

答案 0 :(得分:3)

例如,有https://github.com/mondora/ddp.js/,一个工作同构(浏览器和Node.js)DDP客户端库(我不是这个库的作者,还有一些其他可用的类似的功能)。

您可以轻松连接到任何DDP服务器并收听事件。自述文件中有示例和API文档。另请参阅测试。

使用示例:

服务器代码:

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

客户代码:

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

答案 1 :(得分:3)

我推荐Asteroid,Meteor DDP的客户端软件包:https://github.com/mondora/asteroid

使用示例:

   import {createClass} from "asteroid";


   const Asteroid = createClass();
   // Connect to a Meteor backend
   const asteroid = new Asteroid({
    endpoint: "ws://localhost:3000/ websocket"
    });

   // Use real-time collections
   asteroid.subscribe("tasksPublication");

    asteroid.ddp.on("added", ({collection, id, fields}) => {
    console.log(`Element added to     collection ${collection}`);
    console.log(id);
    console.log(fields);
    });

    // Login
    asteroid.loginWithPassword({username, email, password});

    // Call method and use promises
   asteroid.call("newUser")
    .then(result => {
        console.log("Success");
        console.log(result);
    })
    .catch(error => {
        console.log("Error");
        console.error(error);
    });