#4
Meteor
Asynchronous done right with Meteor
Agenda

intro

problem
solution

outro
My name is
Abderrazak BOUADMA
I’m
Software Engineer (java)
works at
@mbtdoor linkedIn G+
Thanks to
for their support to
Meteor Meetup
intro
Meteor is a platform
built on top of NodeJS
to build

cutting edge fast
web applications.
created to Get Things Done
locked free approach
scalable to the infinity … and beyond
Embraces Simplicity
created to Get Things Done
locked free approach
scalable to the infinity … and beyond
Embraces Simplicity
created to Get Things Done
locked free approach
scalable to the infinity … and beyond
Embraces Simplicity
created to Get Things Done
locked free approach
scalable to the infinity … and beyond
Embraces Simplicity
but ...
still have a long way
to go.
a problem :(
Event Loop

is a programming construct
that waits
for and
dispatches events
or messages in a program
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
Synchronous !
process fork
threads
single thread
while(new Date().getTime() < now + 1000) {
// do nothing
}
all I/O is evented and
asynchronous
var fs = require('fs');
fs.readFile( __dirname + '/test.txt', function (err, data)
{
if (err) {
throw err;
}
console.log(data.toString());
});
db.connect()
conn.query
(“select”)

loop

{

conn.execute(“insert”)

conn.execute(“delete”)
callback pyramid of
a solution :)
Fibers
https://github.com/laverdet/node-fibers
allows us to
write asynchronous
code without
callbacks
futures
var fs = require('fs');
fs.readFile( __dirname + '/test.txt', function (err, data)
{
if (err) {
throw err;
}
});
console.log(data.toString());
var fs = require('fs');
fs.readFile( __dirname + '/test.txt', function (err, data)
{
if (err) {
throw err;
}
});
// console.log(data.toString()); won’t run as the ‘data’
// variable is not accessible at this point.
Solution
var fs = require('fs');
var d;
fs.readFile( __dirname + '/test.txt', function (err, data)
{
if (err) {
throw err;
}
d = data;
});
console.log(d.toString());
Resources
http://bjouhier.wordpress.com/2012/03/11/fibers-and-threads-in-node-js-what-for/
http://meteorhacks.com/fibers-eventloop-and-meteor.html
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
https://github.com/laverdet/node-fibers
Thanks

Meteor and asynchronous done right !