Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible Meteor bug in manual publish/subscribe scenario
    primarykey
    data
    text
    <p>I am encountering a problem with subscribing/publishing in Meteor. I've written an example Meteor app to help narrow the scope of the problem.</p> <p>I am publishing a collection on the server that is filtered by a parameter passed in through a subscription on the client. This subscription is within an autosubscribe, which leverages a session variable to reactively update the subscriptions.</p> <p>When changing the state of this particular session variable, the collection on the client isn't getting updated properly, or at least that's what I gather. I've spent the whole day on this and have not found an issue in code I control. I suspect I'm either not understanding how to setup proper pub-sub in Meteor, or there's a problem within Meteor.</p> <p>To reproduce the problem, start a new Meteor project and use the following (Make sure to remove the autopublish package when trying it out):</p> <p><strong>HTML (test.html for example):</strong></p> <pre><code>&lt;head&gt; &lt;title&gt;pubsubbug&lt;/title&gt; &lt;/head&gt; &lt;body&gt; {{&gt; main}} &lt;/body&gt; &lt;template name="main"&gt; &lt;h1&gt;Example showing possible bug in Meteor wrt pub-sub&lt;/h1&gt; &lt;p&gt;&lt;button name="showall"&gt;show all ({{showall}})&lt;/button&gt;&lt;/p&gt; &lt;div style="float:left;width:400px;"&gt; &lt;h2&gt;Notes:&lt;/h2&gt; &lt;ul&gt; {{#each notes}} &lt;li&gt;{{title}}&lt;/li&gt; {{/each}} &lt;/ul&gt; &lt;/div&gt; &lt;div style="float:left;"&gt; &lt;h2&gt;Notes (copied):&lt;/h2&gt; &lt;ul&gt; {{#each notes_copied}} &lt;li&gt;{{title}}&lt;/li&gt; {{/each}} &lt;/ul&gt; &lt;/div&gt; &lt;/template&gt; </code></pre> <p><strong>JS (test.js for example)</strong></p> <pre><code>if (Meteor.is_client) { Notes = new Meteor.Collection("notes_collection"); NotesCopied = new Meteor.Collection("notes_collection_copied"); Session.set("showall", false); Meteor.autosubscribe(function () { Meteor.subscribe("notes_subscription", Session.get("showall"), function () { console.log("Notes count:", Notes.find().count()); }); Meteor.subscribe("notes_subscription_copied", Session.get("showall"), function () { console.log("Bug? This isn't getting called."); console.log("NotesCopied count:", NotesCopied.find().count()); }); }); Template.main.notes = function () { return Notes.find(); }; Template.main.notes_copied = function () { return NotesCopied.find(); }; Template.main.showall = function () { return Session.get("showall"); }; Template.main.events = { "click button[name='showall']": function (evt) { Session.set("showall", !Session.get("showall")); } }; } if (Meteor.is_server) { Notes = new Meteor.Collection("notes_collection"); var getNotes = function (showall) { if (showall) { return Notes.find({}, {sort: {title: 1}}); } else { return Notes.find({visible: true}, {sort: {title: 1}}); } }; Meteor.publish("notes_subscription", function (showall) { // By sending the Notes back with the same uuid as before, the // client end seems to get confused: return getNotes(showall); }); Meteor.publish("notes_subscription_copied", function (showall) { var notes = getNotes(showall); var self = this; // Copy notes into a new notes collection (see NotesCopied on client). // By generating a new uuid, we don't get an issue with the notes // on the client getting screwed up: notes.forEach(function (note) { var uuid = Meteor.uuid(); // note._id will cause same problem self.set("notes_collection_copied", uuid, {title: note.title}); }); self.flush(); self.complete(); }); // Add example notes Meteor.startup(function () { if (Notes.find().count() === 0) { Notes.insert({title: "Note #1 (always visible)", visible: true}); Notes.insert({title: "Note #2 (always visible)", visible: true}); Notes.insert({title: "Note #3 (always visible)", visible: true}); Notes.insert({title: "Note #4 (only visible when showall true)", visible: false}); Notes.insert({title: "Note #5 (only visible when showall true)", visible: false}); Notes.insert({title: "Note #6 (only visible when showall true)", visible: false}); } }); } </code></pre> <p><em>An explanation of what you will be seeing:</em></p> <p>There will be a button that, when clicked, simply toggles a session variable (showall) between true and false.</p> <p>Two subscriptions exist (within an autosubscribe), one that exemplifies the bug, and another that is suffixed with <code>_copied</code>, which is a test to demonstrate that when the collection in question is "copied" and new uuid's are assigned, the results are displayed properly. I couldn't figure out what to do with this particular bit of info... I don't want new uuid's.</p> <p>So basically, when the <em>show all</em> button is clicked repeatedly, the first column <em>Notes:</em> will display incorrect results, and after a few clicks won't show anything.</p> <p>On the other hand, the second column <em>Notes (copied):</em>, whose uuid's are re-generated each time, shows up correctly.</p> <p>Is this a bug? Or is there a proper way to do this?</p> <p>EDIT: Example above live over at <a href="http://pubsubbug.meteor.com/" rel="nofollow">http://pubsubbug.meteor.com/</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload