Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
536 views
in Technique[技术] by (71.8m points)

meteor - get all subscriber session handles for a publisher method

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

client.js:

col = new Meteor.Collection("data")

Meteor.subscribe("stream")

On server side it looks like

server.js

all_handles = [];

Meteor.publish("stream", function() {
  // safe reference to this sessions
  var self = this;
  // save reference to this subscriber
  all_handles.push(self);
  // signal ready
  self.ready();
  // on stop subscription remove this handle from list
  self.onStop(function() {
    all_handles = _.without(all_handles, self); 
  }
}

Then I can use the all_handles somewhere in my app to send data to those clients, like:

function broadcast(msg) {
  all_handles.forEach(function(handle) {
    handle.added("data", Random.id(), msg);
  }
}

This is already in use and running.

Q: What I am looking for is: Can I get all handles from currently already existing meteor (internal) objects like _sessions or something else?

It would be great if I had not to organize the subscribers handle all the time by myself.

Please do not answer with links to other broadcast packages like streamy or else. I want to continue with standard collections but with as less code as possible.

Thanks for help and feedback Tom

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...