Meteor - get all subscriber session descriptors for the publisher method

I want to cast NON-MONGO-DB data from a server publisher to a client collection. Currently, I keep all registered subscriber pens to use for posting data.

client.js: col = new Meteor.Collection("data") Meteor.subscribe("stream") 

On the 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 all_handles somewhere in my application to send data to these clients, for example:

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

It is already in use and works.

Q: I am looking for: can I get all the pens from existing meteorite objects, such as _sessions or something else?

It would be great if I did not organize subscribers who process all the time themselves.

Please do not respond to links to other broadcast packages such as streamy or else. I want to continue the standard collection, but with less code.

Thanks for the help and feedback Tom

+1
source share
2 answers

As reported by @laberning, I have now used "undocumented" meteorites.

You can publish all subscribers to the publishing method, for example:

 // 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 MeteorPad example for publishing and subscribing and broadcasting

+1
source

Maybe this might work for you: fooobar.com/questions/1015286 / ...

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

+1
source

Source: https://habr.com/ru/post/1015288/


All Articles