real timeninja - codemotion 2015 roma

34
Real Time Ninja Hands On MEAN Giovanni Lela CTO @ LinkMe Cofounder @ MeanMilan Matteo Scandolo MEAN Dev @ LinkMe Cofounder @ MeanMilan

Upload: meanmilan

Post on 14-Feb-2017

143 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Real timeninja - Codemotion 2015 Roma

Real Time NinjaHands On MEAN

Giovanni Lela

CTO @ LinkMe

Cofounder @ MeanMilan

Matteo Scandolo

MEAN Dev @ LinkMe

Cofounder @ MeanMilan

Page 2: Real timeninja - Codemotion 2015 Roma

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

Page 3: Real timeninja - Codemotion 2015 Roma

Pre - nodejs1994 - Netscape enterprise server

1996 - Microsoft Internet Information service

Page 4: Real timeninja - Codemotion 2015 Roma

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

Page 5: Real timeninja - Codemotion 2015 Roma

Hype and dissatisfactionIn the following years node.js gains substantial traction

In 2014, community dissatisfaciton lead to io.jsWhat now?

Page 6: Real timeninja - Codemotion 2015 Roma

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

Page 7: Real timeninja - Codemotion 2015 Roma

Blocking I/O analogyEach cashier (thread) server one

customer at a time

Page 8: Real timeninja - Codemotion 2015 Roma

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

Page 9: Real timeninja - Codemotion 2015 Roma

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

Page 10: Real timeninja - Codemotion 2015 Roma

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)

Page 11: Real timeninja - Codemotion 2015 Roma

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.

Page 12: Real timeninja - Codemotion 2015 Roma

So:Every I/O operation is non blocking

Your code is single threaded

Everything runs in parallel except your code

Page 13: Real timeninja - Codemotion 2015 Roma

"Everything runs in parellelexcept your code"

http://slides.com/adamterlson/promises#/

Page 14: Real timeninja - Codemotion 2015 Roma

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

Page 15: Real timeninja - Codemotion 2015 Roma

Event loopwhile(queue.waitForMessage()){  queue.processNextMessage();}

Page 16: Real timeninja - Codemotion 2015 Roma

Event loop as a languageIn javascript the event loop is not a library it's a language construct

Page 17: Real timeninja - Codemotion 2015 Roma

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}

Page 18: Real timeninja - Codemotion 2015 Roma

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);

Page 19: Real timeninja - Codemotion 2015 Roma

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

Page 20: Real timeninja - Codemotion 2015 Roma

"Everything runs in parellelexcept your code"

http://slides.com/adamterlson/promises#/

Page 21: Real timeninja - Codemotion 2015 Roma

What to do - APIsREST/JSON Api

Plays well with non relational db

LOTS of stable and mature frameworks: hapi, restify, express,loopback

Page 22: Real timeninja - Codemotion 2015 Roma

What to do - Fullstack appsGood ol’ MEAN stack

Isomorphic applications:

Shared models

Shared rendering

Page 23: Real timeninja - Codemotion 2015 Roma

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

Page 24: Real timeninja - Codemotion 2015 Roma

Other interesting fields ofapplication

Desktop Apps

Microservices

Embedded

Page 25: Real timeninja - Codemotion 2015 Roma

Differences from the front endNo DOM api

No browser APIs globals: window, document

No browser inconsistencies: you just get the V8

Page 26: Real timeninja - Codemotion 2015 Roma

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/

Page 27: Real timeninja - Codemotion 2015 Roma

Different globalsprocess

__filename

__dirname

CommonJS globals

global

Page 28: Real timeninja - Codemotion 2015 Roma

Common js globals//hello.js exports.getGreetings = function() {   return 'Hello World'; };

//example.jsvar hello = require('./hello.js');var greetings = hello.getGreetings();

Page 29: Real timeninja - Codemotion 2015 Roma

Built in librariesfs

net

stream

Page 30: Real timeninja - Codemotion 2015 Roma

Many, many callbackscan lead to really confused code

many tools to manage this

some FP is necessary

Page 31: Real timeninja - Codemotion 2015 Roma

And now?Let's Code!

Page 32: Real timeninja - Codemotion 2015 Roma

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!

Page 33: Real timeninja - Codemotion 2015 Roma

Try it on

ninja.link-me.it

Page 34: Real timeninja - Codemotion 2015 Roma

Thanks for listening

Keep in touch

Giovanni Lela: @lambrojos

Matteo Scandolo: @_teone

LinkMe: @LinkMeSRL -

@MM_MeanMilan -

www.link-me.it

github.com/MeanMilan