By

How to use publish and subscribe in Meteor.JS

If you've been on the blogosphere lately, or on Stack Overflow, or on Twitter, or anywhere where anyone is talking about Meteor JS, then you know that the two hottest topics are security and scalability. Security in Meteor can be tricky to get the hang of, but basically, a lot of it hinges on the methods .publish and .subscribe.

The publish method in meteor really doesn't do all to much, but what it does is super, super important. .publish() controls which data can be recieved by the client. Most of what publish does is behind the scenes, but it's important.

Key notes on .publish

  1. Publish works with respect to a session
  2. Publish has the ability to show different things to different people.
  3. Publish works dynamically

For example, if I had a meteor app, and I wrote something like var Posts = new Meteor.Collection("posts"); Meteor.publish("current_users_posts", function() { return Posts.find({ userId: this.userId }); }

We would then be prohibiting any user from accessing any part of the Posts collection that does not belong to them. All we have to do now is subscribe the client side code to the collection.

// In your client code
Meteor.subscribe("current_user_posts");

Meteor will handle the rest. Obviously there is more to be learned, documentation for these methods can be found below.

http://docs.meteor.com/#meteor_publish http://docs.meteor.com/#meteor_subscribe

Written by
Programmer, Entrepreneur, Startup Enthusiast