What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

  • Upload
    ishwil

  • View
    221

  • Download
    0

Embed Size (px)

Citation preview

  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    1/14

    D I S C O V E R M E T E O R Menu

    Reviews

    Case Studies

    Translations

    Encyclopedia

    Podcast

    Blog

    T-Shirt

    Buy the Book

    Log In

    Back to Blog

    JUL 7 2015 by Sacha Greif inARTICLES

    What Goes Where: Making Sense of Meteor's

    Client/Server Split

    https://www.discovermeteor.com/bloghttps://www.discovermeteor.com/bloghttps://www.discovermeteor.com/bloghttps://www.discovermeteor.com/bloghttps://www.discovermeteor.com/blog/what-goes-where/https://www.discovermeteor.com/category/article/https://www.discovermeteor.com/bloghttp://book.discovermeteor.com/https://www.discovermeteor.com/packageshttps://www.discovermeteor.com/tshirthttps://www.discovermeteor.com/bloghttps://www.discovermeteor.com/podcasthttps://www.discovermeteor.com/encyclopediahttps://www.discovermeteor.com/translationshttps://www.discovermeteor.com/case-studieshttps://www.discovermeteor.com/reviewshttps://www.discovermeteor.com/blog/what-goes-where/#https://www.discovermeteor.com/
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    2/14

    One of Meteors biggest selling point is the way it blurs the line between client and server.

    With Meteor, these two environments are no longer separate worlds: they share a common language, and

    can even share common code!

    But that blurring of lines comes at a cost: it can sometimes be tricky to figure out what should run where.

    Client vs Server

    Lets start from the beginning. Meteor runs using the JavaScriptlanguage, which is unique among

    (nearly) every other programming languagesin that it can be executed by browsers. In other words,

    Chrome, Safari, Firefox and all their friends can look at a bit of JavaScript code and make sense of it.

    Try that with Ruby, Python, or C, and your browser will just look at you with a puzzled look on its face.

    But wait! Browsers are not the only ones that can execute JavaScript code. So can servers, thanks to

    Node.js.

    This is where Meteor comes in: by providing layers on top of boththe server and the client, Meteor can

    bridge the gap and make the two sides talk to each other more smoothly.

    Meteors Control Methods

    Meteor gives you three basic ways of controlling where a fileis executed:

    In your app, you can use the /client and /server directoriesto restrict a file to only run in a

    specific environment.

    In packages, you can pass "client" or "server" as argumentsto the api.addFile() function to

    only load a file in a specific environment.

    You can also control where a code blockinsidea file is executed (supposing the file itself runs in both

    environment) thanks to the Meteor.isClient and Meteor.isServer booleans.

    For this article, well only focus on the first scenario: controlling where a file is loaded using the /client

    and /server directories.

    Meteors File Architecture

    Its also important to point out that Meteor doesnt really care whereyou put your code past the

    https://nodejs.org/
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    3/14

    client/server/both distinction. In other words, there is no practical difference between putting

    your code in foo/bar.js and putting it in bar/foo.js .

    That being said, if you need a little more structure we recommend checking out Microscopeto

    see what a typical Meteor app looks like.

    [Client] Browser-Specific JavaScript

    One of the first JavaScript function youll encounter when learning the language is the humble alert() :

    alert("Hello world!");

    You can test this right now if youd like; all it does is pop open a system dialog containing the string you

    passed as argument.

    Now heres a question for you: if I log on to my server, open a Node.js console, and feed it this line of

    JavaScript code, where will the alert show up?

    If your answer was something along the line of What!? That doesnt make any sense! then

    congratulations, you got it right. This was a trick question: alert() is clearly something that only makes

    sense in the context of the browser, since it needs a system to open the dialog, a screen to render it on,

    and a user to close it.

    Now consider the following, used to set the scroll position of the body of a page to 300px from the top:

    $("body").scrollTop(300);

    A perfectly valid bit of JavaScript code (jQuerycode, to be exact), yet one that makes no sense in the

    context of the server. The server has no concept of window, body, or scrolling. Its just a dumb box sitting

    in the middle of some data center!

    Hopefully youre starting to see the pattern: any JavaScript code that specifically targets the browser

    should only run on the client (note that subscriptionsalso fall in this category).

    http://docs.meteor.com/#/full/meteor_publishhttp://jquery.com/http://alert%28%27hello%20world%21%27%29/https://github.com/DiscoverMeteor/Microscope
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    4/14

    The server has no clue what this thing is

    [Client] User-Specific JavaScript

    Additionally, theres another category of code that should only run on the client: code that targets the

    current user.

    Again, it makes sense if you think about it: the server is in charge of serving your app to hundreds, if not

    thousands of users. It doesnt havea current user.

    The Method Exception

    The only exception to the server doesnt know about the user rule is Meteor Methods.

    A Method is a function that runs on the server, but is called from the client. This means the

    server knows which specific usermade the call, and this is whyMeteor.user()

    andMeteor.userId() (as well as the equivalent this.userId ) can be used inside Methods code,

    even on the server.

    http://docs.meteor.com/#/full/meteor_methods
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    5/14

    Theres another Meteor feature thats specific to the current user: Sessionvariables.

    In fact, the same can be said about client-side variables in general: even if client-side variables were

    somehow sent back to the server (and they arent), it wouldnt really make sense. If Alice defines x

    equal to foo but Bob sets it equal to bar , how is the server supposed to know which one to use?

    Alice and Bob arguing about what the value of x should be

    [Client] Templates & Stylesheets

    Although this might change in the future, both templates and stylesheets are currently only executed on

    the client.

    By extension, this also means any template-related JavaScript code (such as template helpers, template

    callbacks, and event handlers) will only be executed on the client as well.

    The one exception to this is emailtemplate: since emails are sent out by the server, you can use a

    package such as handlebars-serverto define specific, server-only templates and styles.

    https://github.com/cmather/meteor-handlebars-serverhttp://docs.meteor.com/#/full/session
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    6/14

    [Both] Collections

    So if templates, stylesheets, template-related code, user-specific code, and browser-related code all run

    on the client, what does that leave for the server?

    A couple important things, as youll soon see. But before we can get there, we need to talk about code

    that needs to run on boththe client andserver.

    The main example of this is collectionscode. Another one of Meteors innovations is the ability to mirror

    some of your data on the client, and this is done through the afore-mentioned collections.

    For that reason, it makes sense to define collections in both environments (in other words, in any

    directory besides /client or /server ). Of course, collection-related code (such as helpers or validation

    code) will also come along for the ride.

    [Both] Methods

    Meteor also features something called optimistic UI (a.k.a. latency compensation). This is a fancy way

    of saying that if an operation needs to involve the server (which would necessarily slow it down), the

    client will try to complete that operation by itself first, and then amend the result if needed once it gets

    the servers actual reply.

    Yet this can only happen if the client knows what the operation consists of, which is why method code is

    often shared among both environments.

    [Server] Secure Data

    Finally, we get to the server, and just the server.

    Unlike the client, the server has direct access to your whole database, which gives it more control over

    your data.

    For example, one of the main tasks of the server is publishingdata to the client. This is done through

    publications, which are server-only. Since publications control which subset of your data should be made

    public (and which subset should remain secure), it stands to reason that this is not something you want

    your users to be able to control.

    In fact, generally speaking any operation that involves private data like email addresses or API keys will

    be best handled by the server. After all, you wouldnt want to expose that data to any random user!

  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    7/14

    [Server] Time-Sensitive Operations

    Finally, another category of server-only operations is anything involving time: its trivial for a user to fake

    their browsers time (not to mention the mess involved with timezones), so you cant rely on it for any

    serious timekeeping.

    So cronjob-type operations that needs to happen at regular intervals, rate-limiting, and timestamping

    should all be left to the server.

    A Real-World Example

    Lets bring all of this together by looking at the code of Microscope, the app that you build in Discover

    Meteor.

    Client

    First, the client directory contains the following items:

    helpers contains global template helpers and code for other client-only features such as error

    messages.

    stylesheets contains the apps styles.

    templates contains the apps templates as well as their companion JavaScript code, split into

    subgroups.

    main.html and main.js hold the apps main template and its code.

    Both

    The lib folder contains files shared across client and server:

    collections contains the code for the Posts, Comments, and Notifications collections.

    permissions.js contains code used to define who can do what, which applies both to client and

    server.

    router.js deals with client-side and server-side routes.

    Server

    And finally, server contains server-only code:

    https://github.com/DiscoverMeteor/Microscope
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    8/14

    fixtures.js contains code that will seed our database with content the first time we run the app.

    pubications.js contains our publication code.

    Conclusion

    This is by no means meant to be an exhaustive list of every single use case youll encounter while

    building Meteor apps.

    So I strongly recommend checking out the Meteor documentationwhenever youre not sure if

    something should run on the client, server, or both.

    But hopefully, it will be enough to give you a few tools to help you figure things out the next time youre

    stuck at that Save As dialog wondering where to put your code.

    Share this article: Tweet !"# %&' ())*+, ,-.',/+,0 23JaimeJaime

    Sacha Greifis a product and web designer who has worked with companies such as

    Hipmunkand Le Monde. He now works on open-source Meteor app Telescope, and

    Sidebar, a design newsletter.

    You can follow him on Twitter.

    ABOUT THE AUTHOR

    Your email

    Get Chapters 1 to 4 of Discover MeteorFor Free

    Leave us your email and you'll receive a free PDFcontaining the first four chaptersof the book

    (Introduction, Getting Started, Deployment, and Templates) to help you get started learning Meteor!

    Get Free Chapters

    FREE STUFF

    READ THIS NEXT

    http://twitter.com/SachaGreifhttp://sidebar.io/http://telesc.pe/http://www.lemonde.fr/http://hipmunk.com/http://sachagreif.com/https://twitter.com/intent/tweet?original_referer=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&ref_src=twsrc%5Etfw&text=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split%20-%20Discover%20Meteor&tw_p=tweetbutton&url=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&via=DiscoverMeteorhttp://docs.meteor.com/
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    9/14

    Jan 2 2014 inARTICLE

    Understanding Meteor Publications & Subscriptions

    The way Meteor handles an apps data is one of the frameworks greatest assets, but also one

    of the hardest things to wrap your head around...Read more

    Allow & Deny: A Security PrimerPREVIOUS ARTICLE

    Building Reusable Components With The

    Template Controller Pattern NEXT ARTICLE

    https://www.discovermeteor.com/blog/meteor-components-template-controller-pattern/https://www.discovermeteor.com/blog/allow-deny-a-security-primer/https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/https://www.discovermeteor.com/blog/understanding-meteor-publications-and-subscriptions/https://www.discovermeteor.com/category/article/
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    10/14

    NeuroNation

    Babbel

    Umfragen Vergleich

    C'est Funny

    BuzzFeed Insider

    PROMIPOOL Retro

    Forget 'Smart Drugs' - this is the right method to boost your intelligence

    Revisiting The Awful German Language

    Es Ist Schockierend, Wieviel Geld Sie Mit Online-Umfragen Verdienen Knnen

    Saviez Vous Que Ces 10 Personnalits taient Gay?

    Top 10 Freaking Tatoos of Celebrities

    James Bond - 007": So sehen die Bond Girls heute aus | www.promipool.de

    15 Comments !1

    ! !

    richylai !

    Publications also know the current user.

    Valentin Zambelli !

    Hey Sacha! I think it is worth mentioning that the server has access to all your data withoutneeding a publication. This means methods that run only on the servercan return data that the

    client is not subscribed to. I made the mistake at first of putting all my methods into the [BOTH]

    directory, which meant I needed to give the client way more access to data than they needed.

    COMMENTS

    https://disqus.com/by/richylai/http://www.promipool.de/bildergalerie-spezial/james-bond-007-so-sehen-die-bond-girls-heute-aus/?utm_campaign=PPBG_bondgirlsdamalsheute&utm_medium=CPC&utm_source=taboola&utm_content=Teaser&utm_source=taboola&utm_medium=referralhttp://buzzfeedinsider.com/2016/05/14/top-10-freaking-tatoos-celebrities/?utm_source=taboola&utm_medium=referralhttp://estfunny.cf/10-stars-gays-dont-vous-ignorez-surement-lhomosexualite/2/?utm_campaign=eFcf-TB-gay.stars-RichEU-desktop&utm_medium=referral&utm_source=taboolahttp://buzzfeedinsider.com/2016/05/14/top-10-freaking-tatoos-celebrities/?utm_source=taboola&utm_medium=referralhttp://estfunny.cf/10-stars-gays-dont-vous-ignorez-surement-lhomosexualite/2/?utm_campaign=eFcf-TB-gay.stars-RichEU-desktop&utm_medium=referral&utm_source=taboolahttps://disqus.com/home/forums/themeteorbook/https://disqus.com/home/inbox/https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#http://www.promipool.de/bildergalerie-spezial/james-bond-007-so-sehen-die-bond-girls-heute-aus/?utm_campaign=PPBG_bondgirlsdamalsheute&utm_medium=CPC&utm_source=taboola&utm_content=Teaser&utm_source=taboola&utm_medium=referralhttps://disqus.com/by/valentinzambelli/https://disqus.com/by/richylai/https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2121005049https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/by/richylai/https://www.discovermeteor.com/blog/what-goes-where/#comment-2121226706https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/home/inbox/https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/home/forums/themeteorbook/http://www.promipool.de/bildergalerie-spezial/james-bond-007-so-sehen-die-bond-girls-heute-aus/?utm_campaign=PPBG_bondgirlsdamalsheute&utm_medium=CPC&utm_source=taboola&utm_content=Teaser&utm_source=taboola&utm_medium=referralhttp://buzzfeedinsider.com/2016/05/14/top-10-freaking-tatoos-celebrities/?utm_source=taboola&utm_medium=referralhttp://estfunny.cf/10-stars-gays-dont-vous-ignorez-surement-lhomosexualite/2/?utm_campaign=eFcf-TB-gay.stars-RichEU-desktop&utm_medium=referral&utm_source=taboolahttp://nachrichten.umfragenvergleich.de/umfragen_B?utm_source=taboola&utm_medium=disqus-widget-safetylevel20longtail07&utm_term=Es+Ist+Schockierend%2C+Wieviel+Geld+Sie+Mit+Online-Umfragen+Verdienen+K%C3%B6nnen&utm_content=http%3A%2F%2Fcdn.taboolasyndication.com%2Flibtrc%2Fstatic%2Fthumbnails%2Fd2da1387c38f400a396b164d18735b49.jpghttps://go.babbel.com/engmag-a93-awfulgerman-tb/1_eng_tab_cd?utm_source=taboola&utm_medium=CON&utm_campaign=cd_engall_gen_cde_awfulgerman&utm_term=disqus-widget-safetylevel20longtail07&utm_content=Revisiting+The+Awful+German+Languagehttps://www.neuronation.com/landingpage/memo-work-a-en/nn-opt/0?lang=en&lp-c=taboola&lp-m=Memo-work-a-en-dach&lp-a=http%3A%2F%2Fcdn.taboolasyndication.com%2Flibtrc%2Fstatic%2Fthumbnails%2F9b6caa71cbcac942860bedb9d35ab3bb.jpg&lp-k=disqus-widget-safetylevel20longtail07&utm_source=taboola&utm_medium=Forget+%27Smart+Drugs%27+-+this+is+the+right+method+to+boost+your+intelligence&utm_campaign=Memo-work-a-en-dach&utm_content=http%3A%2F%2Fcdn.taboolasyndication.com%2Flibtrc%2Fstatic%2Fthumbnails%2F9b6caa71cbcac942860bedb9d35ab3bb.jpg&utm_term=disqus-widget-safetylevel20longtail07
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    11/14

    ! !

    If you only need one thing and it doesn't have to be real time (like the .count() on a whole

    collection) itwouldbe better to write a server only method for it and return the result directly.

    You can even persist it by writing directly to a minimongo collection.

    ! !

    Brad !

    I wasjust looking for some discussion on this. So in the event that you need to

    reference unpublished data, do you use [SERVER] methods, and then when thepublished data is su"cient, run the method in [BOTH]. I've been contemplating if I'm

    approaching my collection operations the "Meteor way" lately... I was debating moving

    methods to the [BOTH] folder but this very subject made me hesitate. I wasn't sure if

    the client stub would just trip up and wait for the server, throw an exception, etc.

    ! !

    Daniel Arrizza !

    When a method is called is run on the client side (the method simulation/stub) doesn't it

    only work with the data that is available on the client? That was my assumption.

    ! !

    Valentin Zambelli !

    Correct, I don't see how this contradicts my statement? That is exactly why I

    advocate having some methods in the [SERVER] scope only. You can access all

    the data you want without having to send it to the client.

    ! !

    cluxter !

    I think I understand what you mean. Let's take the example of a

    chatroom. Alice, Bob and Eve are in the public chatroom. Alice and Bob

    are chatting in a private chatroom. In this case Alice has access to her

    messages only, so does Bob with his messages. But when they are

    chatting, they need to see the messages of each other, without giving Eve

    any access to their messages. So if I understand well, I would put a

    method in [BOTH] that would take care of their respective messages,

    filtering the publication thanks to "userId". They would consequentlyenjoy the latency compensation (but only for their own messages). Then I

    would put a method in [SERVER] which is able to see all of the messages,

    and this method would send the Bob's messages to Alice according to

    what Alice is allowed to see (= the messages they exchanged in their

    private conversation) and vice versa. However Eve will never receive any

    of the messages that Alice and Bob are exchanging, because each user

    has access to its own messages only - the messages from other people

    they are chatting with are like "bonuses" that the server send them

    because they are chatting with someone in private. Is it what you meant?

    Valentin Zambelli !

    https://disqus.com/by/cluxter/https://disqus.com/by/cluxter/https://disqus.com/by/cluxter/https://www.discovermeteor.com/blog/what-goes-where/#comment-2123144655https://www.discovermeteor.com/blog/what-goes-where/#comment-2214173611https://disqus.com/by/valentinzambelli/https://disqus.com/by/valentinzambelli/https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2123076840https://www.discovermeteor.com/blog/what-goes-where/#comment-2123144655https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/by/createnow/https://www.discovermeteor.com/blog/what-goes-where/#comment-2121005049https://www.discovermeteor.com/blog/what-goes-where/#comment-2123076840https://www.discovermeteor.com/blog/what-goes-where/#comment-2121005049https://disqus.com/by/valentinzambelli/https://disqus.com/by/cluxter/https://disqus.com/by/valentinzambelli/https://disqus.com/by/createnow/https://disqus.com/by/disqus_vF8gxUB3V4/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214173611https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214417078https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2123144655https://disqus.com/by/cluxter/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214173611https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2123076840https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2123144655https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2121005049https://disqus.com/by/createnow/https://www.discovermeteor.com/blog/what-goes-where/#comment-2123076840https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2121005049https://disqus.com/by/disqus_vF8gxUB3V4/https://www.discovermeteor.com/blog/what-goes-where/#comment-2295909198https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    12/14

    ! !

    You are still mixing up the concept of pub/sub and methods. "Giving

    access to data" I guess can have two meanings: Let me look at data or

    let me do something with data. Publications/Subscriptions let you look at

    data, so to speak. Methods let you do something with the data. In a

    traditional web application the client would send a GET request to look at

    data and then a POST or PUT request to prompt the server to do

    something with the data.

    The twist with Meteor is that you can cache data on the client and resolve

    POST/PUT requests directly on the client (this is grossly generalized and

    not what Meteor is doing, but in essence I guess it is correct). This is

    done by subscribing to data and putting methods in the [BOTH] scope.

    Your example: What you could do is have 3 collections: one for public

    chatrooms, one for private chatrooms and one for messages. For each

    chatroom you save the IDs of the users that are part of it. When a user

    logs in they would subscribe to all public and private chatrooms they are

    a part of, so that you can display their names in a menubar. When you

    ! !

    cluxter !

    Since client A never sees the notifications of client B this is completely

    unnecessary.

    Couldn't we filter the publication to restrict it to what user A is supposed

    to see, and nothing else?

    ! !

    Valentin Zambelli !

    If we are saving the notifications with the user directly (in an array in the

    profile field for example) you cannot. Lets spin the example further: If you

    want to send a notification email each time an in-app notification isgenerated you will need to access the users email address. But you don't

    want to let users see the email address of other users. To get around the

    need to give users access to the email address of other users you move

    the logic into the [SERVER] scope.

    ! !

    cluxter !

    Got it! Thank you very much for your time and your answers! :-)

    cluxter !

    I still don't et it. What I understood so far was: when a method is in

    https://disqus.com/by/cluxter/https://disqus.com/by/cluxter/https://disqus.com/by/valentinzambelli/https://disqus.com/by/cluxter/https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2123144655https://disqus.com/by/cluxter/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214042429https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2214538023https://disqus.com/by/cluxter/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214654031https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2214478843https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214538023https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2214417078https://disqus.com/by/cluxter/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214478843https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    13/14

    ! !

    [BOTH] scope, you can call it from both server and client. But client will

    only access to data it subscribed to (= only data server decides to

    publish to this client). So there is no risk (as long as the method restricts

    the range of the publication to what the client is allowed to see) that the

    client will see more data than what it is supposed to, right? And

    apparently you agree:

    When a method is called is run on the client side (the methodsimulation/stub) doesn't it only work with the data that is available on

    the client?

    Correct, I don't see how this contradicts my statement?

    So why do you say?:

    You can access all the data you want without having to send it to the

    client.

    Why would I send it to the client? Isn't the method supposed to restrict

    the range of the subscription to what the client is authorized to see? I'm

    confused.

    ! !

    Valentin Zambelli !

    I think you are mixing up 2 concepts. Pub/sub and methods are notdirectly related. Methods do not restrict your access to data per se. (deny

    and allow does that). One thing that I think you are missing is that

    methods in the [SERVER] scope can also be accessed from the client.

    The di#erence is, that you don't need to publish the data that the method

    is working on, since it will only execute in the server environment (i.e.

    directly accessing the database). When a method is in the [BOTH] scope

    you NEED to give the client access to the data, otherwise the method will

    fail.

    Think of it this way: Minimongo is an intelligent cache. When you put a

    method in the [BOTH] scope the client will try to access this cache and

    afterwards tell the server:" Hey, this is what I did, take a look at it." When

    you put it in the [SERVER] scope the client simply tells the server:" Hey,

    please do this thing,thanks a lot" (much like a traditional http requests

    works).

    The upside of having the method in the [BOTH] directory is latency

    compensation: The client sees the change almost instantly, withouthaving to wait for the server to come back with a response. The

    downside is that the client will need to subscribe to the data .

    https://disqus.com/by/valentinzambelli/https://disqus.com/by/cluxter/https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2214042429https://disqus.com/by/valentinzambelli/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214115076https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#
  • 7/25/2019 What Goes Where: Making Sense of Meteor's Client:Server Split - Discover Meteor

    14/14

    Translations| Follow us on Twitter| Like us on Facebook| Contact Us

    Made in Osaka & Melbourne. 2015 Tom Colemanand Sacha Greif

    ! !

    Sacha Greif ! Mod

    Great explanation :)

    ! !

    Daniel Arrizza !

    You're right. I often use the 'isSimulation' property for cases when there's

    server only portions of the method - usually to do with creating activityfeeds/notifications/sending emails.

    ! !

    Sacha Greif ! Mod

    Good point :)

    https://disqus.com/by/folyo/https://disqus.com/by/createnow/https://disqus.com/by/folyo/https://help.disqus.com/customer/portal/articles/1657951?utm_source=disqus&utm_medium=embed-footer&utm_content=privacy-btnhttps://publishers.disqus.com/engage?utm_source=themeteorbook&utm_medium=Disqus-Footerhttps://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2121005049https://disqus.com/by/folyo/https://www.discovermeteor.com/blog/what-goes-where/#comment-2121038100https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2123144655https://disqus.com/by/createnow/https://www.discovermeteor.com/blog/what-goes-where/#comment-2125743510https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://www.discovermeteor.com/blog/what-goes-where/#comment-2214115076https://disqus.com/by/folyo/https://www.discovermeteor.com/blog/what-goes-where/#comment-2214132385https://disqus.com/embed/comments/?base=default&version=b7774234ec45ba8fdcfd0eef50361935&f=themeteorbook&t_i=what-goes-where&t_u=https%3A%2F%2Fwww.discovermeteor.com%2Fblog%2Fwhat-goes-where%2F&t_d=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&t_t=What%20Goes%20Where%3A%20Making%20Sense%20of%20Meteor%27s%20Client%2FServer%20Split&s_o=default#https://mixpanel.com/f/partnerhttp://sachagreif.com/http://tom.thesnail.org/http://discovermeteor.wufoo.com/forms/get-in-touch/http://facebook.com/DiscoverMeteorhttp://twitter.com/DiscoverMeteorhttps://www.discovermeteor.com/translations