How do I handle this tricky pub/sub situation?


#1

I have my client set up to subscribe as follows:

Tracker.autorun(function() {
    if (Session.get('audit-id')) {
        Audit.sub_handle = Meteor.subscribe(
            'audit',
            Session.get('audit-id')
        );
    } else {
        if (Audit.sub_handle) {
            Audit.sub_handle.stop();
        }
    }
});

I have the corresponding publication set up as follows:

Meteor.publish('audit', function(audit_id) {
    if (this.userId) {
        x = Audits.findOne({audit_id: audit_id});
        if (x) {
            widget_ids = d3.set();
            _.each(x.widgets, function(widget) {
                widget_ids.add(widget.widget_id);
            });
            return [
                Audits.find({audit_id: audit_id}),
                Widgets.find(
                    {widget_id: {'$in': widget_ids.values()} }
                ),
            ];
        }
        else {
            console.log('no audits with audit_id: '+audit_id);
        }
    }
});

My problem is that there is no document in the Audits collection with the given audit_id when the client first subscribes.

When I see it run, I see the output “no audits with audit_id: the_audit_id”

However, the document with that audit_id is added a few seconds later (anywhere from a split second later to 8-10 seconds later). I would like the publication to push the data up to the client when the document is added. How can I accomplish this?


#3

You are trying to do a reactive join. This is no easy task with Meteor. See here.


#4

My favorite package for this is https://atmospherejs.com/tmeasday/publish-with-relations


#5

@copleykj Didn’t work for me when using CollectionFS, but [reywood:publish-composite][1] did. Also prefer the API :blush:
[1]: https://atmospherejs.com/reywood/publish-composite