Skip to content

Commit 5dae43d

Browse files
committed
added articles related to the meteorhacks show 1
1 parent b5f04f3 commit 5dae43d

5 files changed

+222
-0
lines changed

_data/navbars.yml

+4
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@
1212
Subscribe for Updates: "http://mad.ly/signups/98366/join"
1313
Tutorials & Articles:
1414
How Loading Time Affects Your Bottom Line: "how-loading-time-affects-your-bottom-line"
15+
Fast Render Internals and How It Works: "http://meteorhacks.com/fast-render-internals-and-how-it-works.html"
16+
How to Test whether Fast Render is Working or Not: "http://meteorhacks.com/how-to-test-fast-render-working-or-not.html"
17+
Integrating Iron Router Based Apps with Fast Render: "http://meteorhacks.com/integrating-iron-router-based-apps-with-fast-render.html"
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
layout: blog
3+
title: "Fast Render Internals and How It Works"
4+
category: blog
5+
summery: "This annotated presentation shows you how Fast Render works and some of the hacks it does."
6+
---
7+
8+
This annotated presentation shows you how Fast Render works and some of the hacks it does.
9+
10+
First of all, let’s see how Meteor normally works.
11+
12+
![How Meteor Normally Works](https://i.cloudup.com/G7FJMkTwwh.jpg)
13+
14+
Normally, Meteor client can’t show anything until it connects to the Meteor server via DDP and receives the data from the server. Depending on the Internet connectivity, this will be an issue, especially in mobile and Wi-Fi.
15+
16+
Fast Render is a solution for this–see how it fixes this issue:
17+
18+
![With Fast Render](https://i.cloudup.com/6azak1msMh.jpg)
19+
20+
Fast Render sends the data required to render the initial page with the HTML, so the browser can render the page without the DDP connection. This provides the same experience as server-side rendering: the user can see the page immediately after HTML has been loaded.
21+
22+
Here is the initial HTML page normally sent by Meteor:
23+
24+
![HTML Comes with Meteor](https://i.cloudup.com/Mluhtxk16m.png)
25+
26+
This is how it looks with Fast Render:
27+
28+
![HTML Comes with Fast Render](https://i.cloudup.com/buJq5psf8o.png)
29+
30+
![Some Hacks I Did](https://i.cloudup.com/fAMz3Md4Y4.jpg)
31+
32+
As you have seen in the screen shots above, Fast Render sends data with the HTML, even though there is no such API available with Meteor to do that. Here is how Fast Render does it:
33+
34+
![Injecting Data into HTML](https://i.cloudup.com/mOlVnB7Lfy.jpg)
35+
36+
Fast Render directly overrides the NodeJS HTTP module, as shown above, to inject the data into the HTML. It is the only possible way to do what Fast Render needs, and works very smoothly.
37+
38+
![DDP Rewrite](https://i.cloudup.com/LQSRhPepzx.jpg)
39+
40+
The next hack I did is the DDP Rewrite, which takes place inside the Meteor client. Let’s see why we need it in the first place. The slide above shows how Meteor subscription works normally.
41+
42+
The **ready** message is useful, since it triggers the Meteor client by saying that the initial data for the subscription has been sent. Now the client can render the page with all the data.
43+
44+
Iron Router is a special case, because it looks for the **ready** message before it renders the templates for the route. In this case, Fast Render has an issue, as shown below:
45+
46+
![DDP Rewrite](https://i.cloudup.com/hLc33Pkpvx.jpg)
47+
48+
With Fast Render, the browser has the data even before sending the subscription request. But Iron Router will not render the route since it didn’t get the ready message. The solution is to send a **fake ready** message, as shown above. The code below shows how Fast Render implements this hack, again without the support of any Meteor API.
49+
50+
![DDP Rewrite - Code](https://i.cloudup.com/myawcxa3fo.jpg)
51+
52+
There are some other hacks involved in building Fast Render. You can learn more about them by looking at the source.
53+
54+
![Security Concerns](https://i.cloudup.com/GT9QuTTDGF.jpg)
55+
56+
Fast Render exposes some security issues that are not normally issues with Meteor. The following section describes what they are and how to prevent them. Some of the issues have been already fixed.
57+
58+
> Our thanks goes to Emily Stark from the Meteor core team for raising these issues.
59+
60+
![LoginToken over Cookies](https://i.cloudup.com/ul8K98Qr78.jpg)
61+
62+
LoginToken is a simple token used to identify the user in the browser. It is identical to the Remember Me cookie used traditionally. Meteor uses localStorage for this purpose instead cookies.
63+
64+
Since localStorage data is never sent to the server, Fast Render cannot identify the logged-in user. The only solution is to send the LoginToken over the cookies. This introduces the following issues:
65+
66+
![Avoid Side Effects](https://i.cloudup.com/vmJyEAw6WN.jpg)
67+
![Avoid Side Effects](https://i.cloudup.com/u98AnBuFRq.jpg)
68+
69+
If your publications trigger side effects as shown above, they may have been triggered by directly calling to route. This can be done via an AJAX request called by a malicious web site. They can’t get the user’s subscription data, but they can trigger side effects. Directly sending an HTTP request can also trigger this.
70+
71+
So it is recommended to avoid side effects, inside publications, Fast Render routes and IronRouter waitOn functions.
72+
73+
![CORS Issue](https://i.cloudup.com/4RCoQWXz9B.jpg)
74+
75+
CORS is a specification that comes with HTML5; it allows cross-domain communication to work inside the browser. When your app or one of your packages adds CORS support, as shown above, it is possible for malicious sites to extract the user’s subscription data by sending an XHR request.
76+
77+
This is not something normally happens, but there is a possibility. In those situations, Fast Render detects it, and turns off Fast Render support for particular routes, _**so there is nothing to worry about.**_
78+
79+
![Questions](https://i.cloudup.com/sJGV6VtABg.jpg)
80+
81+
This article has described how Fast Render actually works and some of its internal workings. If you have any questions, please add a comment: I’d happy to answer.
82+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
layout: blog
3+
title: "How to Test whether Fast Render is Working or Not"
4+
category: blog
5+
summery: "Once you've added Fast Render support, you might not be sure whether Fast Render is correctly applied or not. This article shows you, how to find it out."
6+
---
7+
8+
Once you've added Fast Render support, you might not be sure whether Fast Render is correctly applied or not. Normally it is very hard to detect it locally, since there is not much latency for connecting to the DDP server.
9+
10+
Here is a simple way to check whether you are using Fast Render or not.
11+
12+
![Non Existing DDP Endpoint](https://i.cloudup.com/DaBMkEnXdy.jpg)
13+
14+
Start your app by exporting `DDP_DEFAULT_CONNECTION_URL` with something that does not exist. When you ask the Meteor client to connect to a DDP endpoint that does not exist, it will keep asking for the connection and never get any data from the DDP. It totally depends on FastRender to render the page.
15+
16+
If you've followed the above instructions for your app (and not added Fast Render yet), it should look like this:
17+
18+
![Without Fast Render](https://i.cloudup.com/N8jVxk09tR.jpg)
19+
20+
Once you've added Fast Render support it should look something like this:
21+
22+
![With Fast Render](https://i.cloudup.com/1MohsGbKOg.jpg)
23+
24+
You can verify that the Meteor client is still trying to connect to the DDP server we've provided with `DDP_DEFAULT_CONNECTION_URL`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
layout: blog
3+
title: "Integrating Iron Router Based Apps with Fast Render"
4+
category: blog
5+
summery: "This annotated presentation shows you how to integrate your existing Iron Router based Meteor app with Fast Render."
6+
---
7+
8+
This annotated presentation shows you how to integrate your existing Iron Router based Meteor app with Fast Render.
9+
10+
![Routes on the Server](https://i.cloudup.com/8X9NUx0mvT.jpg)
11+
12+
Fast Render runs your routes on the server. Specifically, it depends on the **waitOn** function on the server side to know which subscriptions are used for the particular route.
13+
14+
So you need to bring your route definitions to a location where they can be accessed by both the server and the client. You also need to guard any client-specific code with **Meteor.isClient**, as shown above.
15+
16+
![Extend with FastRender.RouteController](https://i.cloudup.com/iOOcOfPPyC.jpg)
17+
18+
If you are extending **RouteController**, extend it with FastRender. **RouteController** as shown above. If you are not extending **RouteController** and directly passing **waitOn** function as a route option as shown below, use the fastRender:true option.
19+
20+
![Use fastRender:true](https://i.cloudup.com/Q8prGg7A7S.jpg)
21+
22+
If you are using **this.subscribe(‘foo’).wait()** inside a before handler, Fast Render cannot understand them automatically, since before handlers contain a lot of client-side logics. So it is not a good option to run them inside the server.
23+
24+
Therefore, you need to specify subscriptions manually, as shown below:
25+
26+
![](https://i.cloudup.com/cHRnXaqNZ9.jpg)
27+
28+
**FastRender.onAllRoutes** can be used if **canView** is a global before handler. If it is a before handler for a specific route, use **FastRender.route** as shown below:
29+
30+
![](https://i.cloudup.com/YLG52kb6QY.jpg)
31+
32+
You might have to assign the return value of **Meteor.subscribe** directly into the **waitOn**. But in this case, Fast Render cannot detect the subscriptions, so the correct solution is to wrap the subscription with a function.
33+
34+
![Use waitOn() with Functions](https://i.cloudup.com/p3Hgtpt9EW.jpg)
35+
36+
Hope this article helps you to integrate your Meteor app with Fast Render correctly with less issues. If you have any questions, please add a comment: I’d happy to answer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
---
2+
layout: blog
3+
title: "Meteor Subscription Optimization"
4+
category: blog
5+
summery: "This annotated presentation shows you how to optimize subscriptions while keeping your both users and servers happy at the same time."
6+
---
7+
This annotated presentation shows you how to optimize subscriptions while keeping your both users and servers happy at the same time.
8+
9+
Subscriptions management is a common issue with Meteor and sometimes introduces weird issues. That's why we need to take care of optimizing subscriptions. This guide will give you some tips on where you can optimize your subscriptions effectively.
10+
11+
![Subscriptions on the Server](https://i.cloudup.com/EQpEAZ7oaB.jpg)
12+
13+
First we need to understand how subscriptions are handled on the server, which will help us to make good decisions.
14+
15+
![Grouped by Query](https://i.cloudup.com/agZPzf3NxG.jpg)
16+
17+
In the above publication, a static query has been used. It doesn't change with the arguments from the subscription, so it creates only one **LiveResultSet**. Even if you had 20 subscriptions, there would be only one **LiveResultSet**. This is an advantage because, when a polling takes place, there will be less overhead.
18+
19+
![Grouped by Query](https://i.cloudup.com/yxd8LQDty0.jpg)
20+
21+
In contrast to the previous publication, this one creates a LiveResultSet for every subscription. So when the polling takes place, it will be much costlier, since we will have many LiveResults.
22+
23+
Practically, it is not possible to avoid these kinds of scenarios all the time. That's why oplog integration plays such a huge role: it doesn't do polling and makes subscriptions lightweight in the server
24+
25+
![Data is Cached for Each Session](https://i.cloudup.com/U79xqDmKKw.jpg)
26+
27+
The Meteor server keeps a copy of data (documents) available on each client. It allows Meteor to make better decisions when sending changes to the client. But it also increases the RAM usage, if your data set is large.
28+
29+
![Deps.autorun and Subscriptions](https://i.cloudup.com/xhfoFcwNMT.jpg)
30+
31+
Next, we need to understand how subscriptions behave in the client, especially inside a `Deps.autorun` computation.
32+
33+
![After an Invalidation](https://i.cloudup.com/T0xFW7pQx9.jpg)
34+
35+
In the first invalidation round, slug is **meteorhacks**. It adds two subscriptions to singlePost and postList.
36+
37+
In the next invalidation, slug is **oplog**. The computation removes the old subscription to **singlePost**, with **meteorhacks** as the argument. After that it adds a subscription to singlePost, with oplog as the argument. It does not change the existing subscription to **postList**.
38+
39+
This feature is one of the core concepts underlying Iron Router's subscription model.
40+
41+
![Relation with Iron Router](https://i.cloudup.com/Wm1Lgz7nnc.jpg)
42+
43+
Iron Router runs on a Single Computation. So when it switches between routes, all the subscriptions made from the previous route are removed, **unless they are used in the current route**.
44+
45+
![Three Simple Optimization Rules](https://i.cloudup.com/nHszCg13zG.jpg)
46+
47+
Here are three simple subscription optimization rules that will help you build better applications while keeping both users and servers happy.
48+
49+
![Send data only the page/client requires](https://i.cloudup.com/iJGTtxKRRc.jpg)
50+
51+
We should send only the data that a page or user requires. The page will load faster, and less overhead (in terms of memory usage) will be added to the server.
52+
53+
![Don't unsubscribe busy subscriptions](https://i.cloudup.com/4KUbHwKgUC.jpg)
54+
55+
Some subscriptions are meant to be used in multiple pages/routes of your app, so it is wise to keep them open. Otherwise there will be some bandwidth issues and users will have to wait while data gets loaded from the server again.
56+
57+
![Break the rules as needed](https://i.cloudup.com/FVacZRQdod.jpg)
58+
59+
These are not hard and fast rules: break them whenever necessary.
60+
61+
## Case Studies (Atmosphere & Telescope)
62+
63+
This is a case study where I review Atmosphere and Telescope in terms of subscription usage.
64+
65+
<iframe width="640" height="480" src="//www.youtube.com/embed/nkWtsLjLtmc?rel=0" frameborder="0" allowfullscreen="1">
66+
</iframe>
67+
68+
## Sneak Peak: Subscription Manager
69+
70+
Subscription Manager is a simple package that allows you to avoid some of the subscription-related issues described in the case study above.
71+
72+
<iframe width="640" height="480" src="//www.youtube.com/embed/xzPg0-_TcXU?rel=0" frameborder="0" allowfullscreen="1">
73+
</iframe>
74+
75+
I hope these suggestions will be helpful as you build better quality Meteor applications.
76+

0 commit comments

Comments
 (0)