在浏览器中独立连接Meteor DDP

Connecting to Meteor DDP standalone in browser?

本文关键字:Meteor DDP 连接 独立 浏览器      更新时间:2023-09-26

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

我找到了Meteor DDP包,上面写着这是可以做到的。

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

或者你知道其他适用于浏览器的DDP客户端吗?

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

您可以轻松连接到任何DDP服务器并监听事件。Readme文件中有示例和API文档。还要看测试。

示例用法:

服务器代码:

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

客户代码:

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

我推荐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);
    });