Tutorial by Examples

First, remove autopublish. autopublish automatically publishes the entire database to the client-side, and so the effects of publications and subscriptions cannot be seen. To remove autopublish: $ meteor remove autopublish Then you can create publications. Below is a full example. import { Mon...
A global publication does not possess a name and does not require a subscription from the connected client and therefore it is available to the connected client as soon as the client connects to the server. To achieve this, one simply names the publication as null like so Meteor.publish(null, func...
A named publication is one that possesses a name and needs to be explicitly subscribed to from the client. Consider this server side code: Meteor.publish('somePublication', function() { return SomeCollection.find() }) The client needs to request it by: Meteor.subscribe('somePublication') ...
Meteor's default templating system Spacebars and its underlying rendering subsystem Blaze integrate seemlessly with publication lifecycle methods such that a simple piece of template code can subscribe to its own data, stop and clean up its own traces during the template tear down. In order to tap ...
For if you have to fine-tune what is published. import { Mongo } from 'meteor/mongo'; import { Meteor } from 'meteor/meteor'; import { Random } from 'meteor/random'; if (Meteor.isClient) { // established this collection on the client only. // a name is required (first parameter) and this...
On the server, you can create a publication like this. this.userId is the id of the user who is currently logged in. If no user is logged in, you might want to throw an error and respond to it. import Secrets from '/imports/collections/Secrets'; Meteor.publish('protected_data', function () { ...
A template autorun may be used to (re)subscribe to a publication. It establishes a reactive context which is re-executed whenever any reactive data it depends on changes. In addition, an autorun always runs once (the first time it is executed). Template autoruns are normally put in an onCreated met...
Template JS code Template.templateName.onCreated(function(){ this.subscribe('subsription1'); this.subscribe('subscription2'); }); Template HTML code <template name="templateName"> {{#if Template.subscriptionsReady }} //your actual view with data. it can ...
Sometimes it's a good idea to further secure your publishes by requiring a user login. Here is how you achieve this via Meteor. import { Recipes } from '../imports/api/recipes.js'; import { Meteor } from 'meteor/meteor'; Meteor.publish('recipes', function() { if(this.userId) { return Re...
Multiple database cursors can be published from the same publication method by returning an array of cursors. The "children" cursors will be treated as joins and will not be reactive. Meteor.publish('USER_THREAD', function(postId) { let userId = this.userId; let comments = Comm...
In real world, connection and server delays could occur, to simulate delays in development environment Meteor._sleepForMs(ms); could be used Meteor.publish('USER_DATA', function() { Meteor._sleepForMs(3000); // Simulate 3 seconds delay return Meteor.users.find({}); });
Publications can be merged on the client, resulting in differently shaped documents within a single cursor. The following example represents how a user directory might publish a minimal amount of public data for users of an app, and provide a more detailed profile for the logged in user. // client...

Page 1 of 1