簡體   English   中英

在meteor中,pub / sub可以用於任意的內存中對象(不是mongo集合)

[英]In meteor, can pub/sub be used for arbitrary in-memory objects (not mongo collection)

我想在我的流星應用程序中建立雙向(雙向)通信。 但是我需要在不使用mongo集合的情況下完成它。

那么pub / sub可以用於任意的內存中對象嗎?

是否有更好,更快或更低級別的方式? 表現是我​​最關心的問題。

謝謝。

是的,pub / sub可用於任意對象。 Meteor的文檔甚至提供了一個例子

// server: publish the current size of a collection
Meteor.publish("counts-by-room", function (roomId) {
  var self = this;
  check(roomId, String);
  var count = 0;
  var initializing = true;

  // observeChanges only returns after the initial `added` callbacks
  // have run. Until then, we don't want to send a lot of
  // `self.changed()` messages - hence tracking the
  // `initializing` state.
  var handle = Messages.find({roomId: roomId}).observeChanges({
    added: function (id) {
      count++;
      if (!initializing)
        self.changed("counts", roomId, {count: count});
    },
    removed: function (id) {
      count--;
      self.changed("counts", roomId, {count: count});
    }
    // don't care about changed
  });

  // Instead, we'll send one `self.added()` message right after
  // observeChanges has returned, and mark the subscription as
  // ready.
  initializing = false;
  self.added("counts", roomId, {count: count});
  self.ready();

  // Stop observing the cursor when client unsubs.
  // Stopping a subscription automatically takes
  // care of sending the client any removed messages.
  self.onStop(function () {
    handle.stop();
  });
});

// client: declare collection to hold count object
Counts = new Mongo.Collection("counts");

// client: subscribe to the count for the current room
Tracker.autorun(function () {
  Meteor.subscribe("counts-by-room", Session.get("roomId"));
});

// client: use the new collection
console.log("Current room has " +
            Counts.findOne(Session.get("roomId")).count +
            " messages.");

在此示例中, counts-by-room是發布從Messages.find()返回的數據創建的任意對象,但您可以輕松地將源數據放在其他位置並以相同的方式發布。 您只需要提供相同的addedremoved回調,例如此處的示例。

您會注意到在客戶端上有一個名為counts的集合,但這純粹是在客戶端內存中; 它沒有保存在MongoDB中。 我認為這是使用pub / sub的必要條件。

如果你想避免使用僅限內存的集合,你應該看看Meteor.call 您可以像getCountsByRoom(roomId)一樣創建Meteor.method ,並從客戶端調用它,如Meteor.call('getCountsByRoom', 123) ,該方法將在服務器上執行並返回其響應。 這更像是傳統的Ajax做事方式,而你失去了Meteor的所有反應。

只是添加另一個簡單的解決方案 您可以將connection: null傳遞給服務器上的Collection實例化。 即使這沒有詳細記錄,但我從流星人那里聽說這使得這個集合在內存中。

這是一年前由Emily Stark發布的示例代碼:

if (Meteor.isClient) {
  Test = new Meteor.Collection("test");
  Meteor.subscribe("testsub");
}

if (Meteor.isServer) {
  Test = new Meteor.Collection("test", { connection: null });
  Meteor.publish("testsub", function () {
    return Test.find();
  });

  Test.insert({ foo: "bar" });
  Test.insert({ foo: "baz" });
}

編輯

這應該是評論,但我發現它可能太長了所以我發布作為答案。 或許我誤解了你的問題?

我想知道你為什么反對mongo 我莫名其妙地發現它與Meteor很匹配。

無論如何,每個人的用例可能不同,你的想法是可行的,但不是一些嚴重的黑客。

如果你看一下Meteor源代碼,你可以找到tools/run-mongo.js ,這是Meteormongo交談的地方,你可以調整或實現​​你的適配器來處理內存中的對象。

我能想到的另一種方法是包裝你的內存中對象並編寫數據庫邏輯/層來攔截現有的mongo數據庫通信(27017上的默認端口),你必須處理所有系統環境變量,如MONGO_URL等。使其正常工作。

最后的方法是等到Meteor正式支持像Redis這樣的其他數據庫。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM