Real timeninja - Codemotion 2015 Roma
-
Upload
meanmilan -
Category
Technology
-
view
143 -
download
2
Transcript of Real timeninja - Codemotion 2015 Roma
Real Time NinjaHands On MEAN
Giovanni Lela
CTO @ LinkMe
Cofounder @ MeanMilan
Matteo Scandolo
MEAN Dev @ LinkMe
Cofounder @ MeanMilan
What's nextWhen / why did it start
Extremely boring theory
What to do aka common use cases
Differences from client side javascript
App example: a RESTful ninja managament tools
with real time notifications
Pre - nodejs1994 - Netscape enterprise server
1996 - Microsoft Internet Information service
Enter node jsIn 200* Ryan Dahl saw a polling progress bar
He wanted a super easy way to implement sites with pushcapabilities
In 2009 node.js was born
Joyent takes control
Hype and dissatisfactionIn the following years node.js gains substantial traction
In 2014, community dissatisfaciton lead to io.jsWhat now?
Why should I care?It can can handle a lot of concurrent I/O operations
Because of non blocking I/O and javascript's own nature
Blocking I/O analogyEach cashier (thread) server one
customer at a time
Blocking I/O analogy (2)The cashier waits for your food
to be cooked before doinganything else (he’s blocked)
The cashier is not able to help thenext person until you got your
food and went your way
Blocking I/O analogy (3)The more customers you want to serve at once the more cashier
lines you need
Cashier lines -> thread
Multi-threaded parellel execution
Non-blocking I/O analogyAs soon as you place your order, it’s sent off for someone to fulfill
while the cashier is still taking your payment.
When you are done paying, you have to step aside because thecashier is already looking to service the next customer.
When your food is set, the cashier – or someone – will signal you bycalling out your name, (callback)
Non-blocking I/O (2)You use your web browser to make a request for “/about.html” on
a Node.js web server.
The Node server accepts your request and calls a function toretrieve that file from disk.
While the Node server is waiting for the file to be retrieved, itservices the next web request.
When the file is retrieved, there is a callback function that isinserted in the Node servers queue.
The Node server executes that function which in this case wouldrender the “/about.html” page and send it back to your web browser.
So:Every I/O operation is non blocking
Your code is single threaded
Everything runs in parallel except your code
"Everything runs in parellelexcept your code"
http://slides.com/adamterlson/promises#/
Event loopA JavaScript runtime contains an event queue, which is a list of
tasks to be processed
Each message event is bound to a callback function
The said runtime loops on the queue, executing all the event callbacks
Event loopwhile(queue.waitForMessage()){ queue.processNextMessage();}
Event loop as a languageIn javascript the event loop is not a library it's a language construct
Event loop in ruby /eventmachine
require 'eventmachine'
module Echo def post_init puts "Connection Connected Yo" end def receive_data send_data "#{data}" close_connection if data =~ /quit/if endend
EventMachine.run { EventMachine.start_server "127.0.0.1", 8081, Echo}
Event loop in node.js (and es6!)require('net').createServer((socket)=>{
console.log('Connection connected'); socket.on('data', req => socket.write(req.toString()))}).listen(8000);
What not to doeverything that is CPU intensive
image/video processing
heavyweight number crunching
compressing files
everything involving complex algorithms
Event loops are highly specialized and not general purpose
"Everything runs in parellelexcept your code"
http://slides.com/adamterlson/promises#/
What to do - APIsREST/JSON Api
Plays well with non relational db
LOTS of stable and mature frameworks: hapi, restify, express,loopback
What to do - Fullstack appsGood ol’ MEAN stack
Isomorphic applications:
Shared models
Shared rendering
What to do - Real time webapps
It was born for this
Lots of stable mature frameworks (again) - primus, socket I/O ecc
It ca ben integrated into an existing web stack
http://webandphp.com/IntegratingNode.jswithPHP
Other interesting fields ofapplication
Desktop Apps
Microservices
Embedded
Differences from the front endNo DOM api
No browser APIs globals: window, document
No browser inconsistencies: you just get the V8
Excellent parts of javascriptsArray.prototype.forEach(), Array.prototype.map(), Object.keys() [...]
http://kangax.github.io/compat-table/es5/
http://kangax.github.io/compat-table/es6/
Different globalsprocess
__filename
__dirname
CommonJS globals
global
Common js globals//hello.js exports.getGreetings = function() { return 'Hello World'; };
//example.jsvar hello = require('./hello.js');var greetings = hello.getGreetings();
Built in librariesfs
net
stream
Many, many callbackscan lead to really confused code
many tools to manage this
some FP is necessary
And now?Let's Code!
What are we going to do?Setup the environment
Connect to MongoDB
Define "Models"
Define Routes and Middlewares
Implement a basic CRUD
A basic FrontEnd
And finally... Realtime!
Thanks for listening
Keep in touch
Giovanni Lela: @lambrojos
Matteo Scandolo: @_teone
LinkMe: @LinkMeSRL -
@MM_MeanMilan -
www.link-me.it
github.com/MeanMilan