meteor - get all subscriber session handles for a publisher method

后端 未结 2 1171
北海茫月
北海茫月 2020-12-20 08:45

I want to broadcast NON-MONGO-DB data from a server publisher to client collections. Currently I save all registered subscriber handles to use those for posting the data

相关标签:
2条回答
  • 2020-12-20 09:09

    As informed by @laberning I used for now the "undocumented" meteor connections.

    You can post to all subscribers of a publishing method like:

    // publish updated values to all subscribers
    function publish_to_all_subscribers(subscription_name, id, data) {
      _.each(Meteor.server.stream_server.open_sockets, function(connection) {
        _.each(connection._meteorSession._namedSubs, function(sub) {
          if (sub._name == subscription_name) {
            sub.insert(subscription_name, id, data);
          }
        })
      });
    }
    
    // create stream publisher
    Meteor.publish('stream', function(){
      // set ready
      this.ready();
    });
    
    ...
    // use publishing somewhere in your app
    publish_to_all_subscribers('stream', Random.id(), {msg: "Hello to all"});
    ...
    

    updated: See an example MeteorPad for Publish and Subscribe and Broadcast messages

    0 讨论(0)
  • 2020-12-20 09:20

    Maybe this might work for you: https://stackoverflow.com/a/30814101/2005564

    You could get the connections via var connections = Meteor.server.stream_server.open_sockets; but as looshi said this might break with a future meteor update as it is not part of the public API...

    0 讨论(0)
提交回复
热议问题