Meteor.defer() and this.unblock()

This blog post is about performance gains with the help of Meteor.defer() and this.unblock().
 We’ll see how those two methods work and what’s the difference between the two.

This is a free sample taken from “Auditing Meteor applications”

Meteor.defer()

You might have some tasks in your application that take longer to process. If you e.g. have a method which sends an E-Mail at the end of the method call you end up waiting until the E-Mail is finally sent.

This can slow down your app dramatically. But Meteor has you covered. There’s a functionality called Meteor.defer() which makes it possible to run the code asynchronously in the background. Let’s take a look at a practical example:

Let’s pretend we have a method which sends an E-Mail after the user has signed up (e.g. a “Welcome aboard E-Mail”).
 Our first try might look something like this:

Meteor.methods({<br />
signUp: function(user) {<br />
// some code to create the new user<br />
Email.send(/* email object */);<br />
}<br />
});<br />

But this call of Email.send() will block the code because it’s executed synchronously.

Let’s use Meteor.defer() to run the E-Mail sending asynchronously and speed up the whole process:

Meteor.methods({<br />
signUp: function(user) {<br />
// some code to create the new user<br />
Meteor.defer(function () {<br />
Email.send(/* email object */);<br />
});<br />
}<br />
});<br />

We’ve wrapped the E-Mail sending code inside Meteor.defer(). This tells Meteor to run the code asynchronously in the background. The execution won’t be blocked and our application will respond instantly after the user has signed up.

this.unblock()

Meteor uses DDP (Distributed data protocol) messages to communicate between client and server. The DDP messages are processed in the order they are initiated.

This is important so that sequences of operations are processed in the correct order and no error occurs. Let’s imagine you update a post and after that remove it. Meteor needs to process the operations in that way because it’s not possible to update a previously removed post.

this.unblock() makes it possible to break this „in order processing“. This is especially useful if you e.g. have an API call which takes quite some time.

Let’s see how to use this.unblock()

Let’s pretend we have a method with an API call which takes very long. Now we can add this.unblock(); in the beginning so that Meteor does not wait for the API call to finish and starts the processing of the next DDP messages instantly:

Meteor.methods({<br />
getProfile: function(email) {<br />
this.unblock();<br />
var profileInformation = HTTP.get('http://example.com/?email=' + email);<br />
return profileInformation;<br />
}<br />
});<br />

note:
 You can not use this.unblock(); every time. You should e.g. not use this.unblock(); if you rely on the fetched data in another method.

The difference between Meteor.defer() and this.unblock()

After we’ve seen how Meteor.defer() and this.unblock() works we can tell the difference easily. Meteor.defer() let’s you run your code asynchronously in the background whilst this.unblock() makes it possible to skip DDP messages which take long to process and move forward to the next DDP message in the queue.

They are both independent methods which helps you to speed up your application.