15
1 - CSE 330 – Creative Programming and Rapid Prototyping Module 6 – Node.js and Socket.IO Module 6 Contains 2 components – Individual Assignment and Group Assignment • Both are due on Wednesday April 4 th Read the WIKI before starting Portions of today’s slides came from – Craig Hecock – Iván Loire – Roberto Peon – Charles Wang 2 - CSE 330 – Creative Programming and Rapid Prototyping Preview of Creative Project – Due 4/25/18 Come up with an idea utilizing skills learned from previous modules – Should be comparable to work of previous modules – You may work alone or in a group on this module Create your Creative Project repo and push to it your project description along with a grading rubric after it is approved by a TA Rubric is due Wednesday April 4th by the end of class – The project is worth 100 points, 5 of which come from submitting this rubric to Bitbucket • You are allowed to assign up to 20 points for a creative portion • Create a file named gradingRubric.md inside your Bitbucket repo • Include the name of the TA that approved your rubric

Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Embed Size (px)

Citation preview

Page 1: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 11 - CSE 330 – Creative Programming and Rapid Prototyping

Module6– Node.jsandSocket.IO• Module6Contains2components

– IndividualAssignmentandGroupAssignment• BotharedueonWednesdayApril4th

• ReadtheWIKIbeforestarting

• Portionsoftoday’sslidescamefrom– CraigHecock– Iván Loire– RobertoPeon– CharlesWang

Extensible Networking Platform 22 - CSE 330 – Creative Programming and Rapid Prototyping

PreviewofCreativeProject– Due4/25/18

• Comeupwithanideautilizingskillslearnedfrompreviousmodules– Shouldbecomparabletoworkofpreviousmodules– Youmayworkaloneorinagrouponthismodule

• CreateyourCreativeProjectrepoandpushtoityourprojectdescriptionalongwithagradingrubricafteritisapprovedbyaTA

• RubricisdueWednesdayApril4thbytheendofclass– Theprojectisworth100points,5ofwhichcomefromsubmittingthisrubrictoBitbucket• Youareallowedtoassignupto20pointsforacreativeportion• CreateafilenamedgradingRubric.md insideyourBitbucket repo• IncludethenameoftheTAthatapprovedyourrubric

Page 2: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 33 - CSE 330 – Creative Programming and Rapid Prototyping

CSE503SPerformanceEvaluationReport

• 503Sstudentswillcompleteaperformanceevaluationstudyoftheirwebserver

• Theevaluationwillincludetwoexperimentsthatmeasurecriticalresourcesusedthroughoutthesemester– Forexample,IfmyappusesApacheandMySQL

• Howmanypages/seccanApacheserve?• Howmanyreads/secandwrites/seccanmydatabaseperform?

– Thewikiprovidesalistofpotentialexperiments

• AperformanceevaluationproposalisdueonApril4th (alongwithyourCreativeProjectDescription)– CreateaPerformanceEvaluationRepousingthelinkprovidedonPiazza– Listtheexperimentsyouplantoperformandwhytheseareimportanttostudy– CreateafileinsideyourPerformanceEvaluationReponamed

proposalExperiments.md explainingyourexperimentsbyApril4th

• Studentswillsubmitawrittendocumentexplainingtheexperimentsalongwiththeirresultsby11:59PMonFridayApril27th

• Refertothecoursewebsiteforadditionaldetailsaboutthereport– https://classes.engineering.wustl.edu/cse330/index.php/CSE_503S_Performance_Evaluation_Study

Extensible Networking Platform 44 - CSE 330 – Creative Programming and Rapid Prototyping

CSE503SPerformanceEvaluationExample

Page 3: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 55 - CSE 330 – Creative Programming and Rapid Prototyping

WhatisNode.js?

• AJavaScriptruntimeenvironmentrunningGoogleChrome’sV8engine– a.k.a.aserver-sidesolutionforJS– CompilesJS,makingitreallyfast

• Runsoverthecommandline

• Designedforhighconcurrency– Withoutthreadsornewprocesses

• Neverblocks,notevenforI/O

Extensible Networking Platform 66 - CSE 330 – Creative Programming and Rapid Prototyping

Concurrency:TheEventLoop

• InsteadofthreadsNodeusesaneventloopwithastack

• Alleviatesoverheadofcontextswitching

Page 4: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 77 - CSE 330 – Creative Programming and Rapid Prototyping

Non-blockingI/O

• ServersdonothingbutI/O– ScriptswaitingonI/Orequestsdegradesperformance

• Toavoidblocking,NodemakesuseoftheeventdrivennatureofJSbyattachingcallbackstoI/Orequests

• ScriptswaitingonI/Owastenospacebecausetheygetpoppedoffthestackwhentheirnon-I/Orelatedcodefinishesexecuting

Extensible Networking Platform 88 - CSE 330 – Creative Programming and Rapid Prototyping

I/OExample

Page 5: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 99 - CSE 330 – Creative Programming and Rapid Prototyping

Extensible Networking Platform 1010 - CSE 330 – Creative Programming and Rapid Prototyping

Page 6: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 1111 - CSE 330 – Creative Programming and Rapid Prototyping

Threads VS Event-driven

Threads Asynchronous Event-driven

Lock application / request with listener-workers threads

only one thread, which repeatedly fetches an event

Using incoming-request model Using queue and then processes it

multithreaded server might block the request which might involve multiple events

manually saves state and then goes on to process the next event

Using context switching no contention and no context switches

Using multithreadingenvironments where listener and workers threads are used frequently to take an incoming-request lock

Using asynchronous I/O facilities (callbacks, not poll/select or O_NONBLOCK) environments

Extensible Networking Platform 1212 - CSE 330 – Creative Programming and Rapid Prototyping

Node.js VS Apache

Platform Number of request per second

PHP ( via Apache) 318,727

Static ( via Apache ) 296,651

Node.js 556,930

Page 7: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 1313 - CSE 330 – Creative Programming and Rapid Prototyping

Node.js HelloWorld(hello.js)

var http=require('http');

http.createServer(function (req,res){res.writeHead(200,{'Content-Type':'text/plain'});res.end('Hello,world!');

}).listen(80);

Extensible Networking Platform 1414 - CSE 330 – Creative Programming and Rapid Prototyping

Node.jsExamples

Page 8: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 1515 - CSE 330 – Creative Programming and Rapid Prototyping

Module6IndividualDemo

Extensible Networking Platform 1616 - CSE 330 – Creative Programming and Rapid Prototyping

Socket.IO

Page 9: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 1717 - CSE 330 – Creative Programming and Rapid Prototyping

WebSockets

• Transportprotocolforwebcommunication– AlternativetotheXMLHttpRequestobjectusedforAJAX

• ProvidesBi-directional,fullduplexcommunicationchannel

• Designedforimplementationinwebbrowsersandservers

• Extremelylowoverheadforpayload,justtwobytes

• APIstandardizedbyW3C

Extensible Networking Platform 1818 - CSE 330 – Creative Programming and Rapid Prototyping

TCP handshakeHTTP Headers (request)

HTTP Headers (response)

“hello, my name is Chrome, encoding UTF-8... I would like a web page please.”

+ +

HTTPOverhead(foreachrequest)

Page 10: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 1919 - CSE 330 – Creative Programming and Rapid Prototyping

•Page Load Time (PLT) is our measure of latency.

• Note the diminishing returns as bandwidth increases

Dofasterconnectionshelp?

Extensible Networking Platform 2020 - CSE 330 – Creative Programming and Rapid Prototyping

•Now we vary the Round Trip Time (RTT) for a fixed bandwidth.

•Reducing RTT, always helps reduce Page Load Time (PLT).

Docloserserverhelp?

Page 11: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 2121 - CSE 330 – Creative Programming and Rapid Prototyping

data + 2 byte overhead

data + 2 byte overhead

BrowserServer

WebSockets

TCP handshake(just first request)

• HelpreducethenumberofRoundTripTimes(RTTs)

Extensible Networking Platform 2222 - CSE 330 – Creative Programming and Rapid Prototyping

http://caniuse.com/#feat=websockets

Page 12: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 2323 - CSE 330 – Creative Programming and Rapid Prototyping

DatafromOct29th 2014

Extensible Networking Platform 2424 - CSE 330 – Creative Programming and Rapid Prototyping

Socket.IO

• Websocketsforall– Websocketsarenotsupportedinallbrowsers

• SocketIOsupportsavarietyoftransports– HTML5WebSocket– FlashSocket– AJAXLongPolling– ForeverIframe

Page 13: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 2525 - CSE 330 – Creative Programming and Rapid Prototyping

GettingStartedwithSocket.IO

• Socket.IO:http://socket.io

• InstallSocket.IO withnpm:– npm installsocket.io

NodeexampleusingSocket.IO:var app=require('http').createServer(callback);var io =require('socket.io').listen(app);app.listen(8080);

Extensible Networking Platform 2626 - CSE 330 – Creative Programming and Rapid Prototyping

Socket.IO Server

io.sockets.on('connection',function(socket){

socket.emit('Hellofromserver',{hello:'world'});

socket.on('Replyfromclient',function(data){console.log(data);});});

Page 14: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 2727 - CSE 330 – Creative Programming and Rapid Prototyping

Socket.IO Client(index.html)

<scriptsrc="/socket.io/socket.io.js"></script>

<script>var socket=io.connect('http://localhost');socket.on('Hellofromserver',function(data){

console.log(data);socket.emit('Replyfromclient',{hello:'fromclient'});

});

</script>

Extensible Networking Platform 2828 - CSE 330 – Creative Programming and Rapid Prototyping

SimpleSocket.IODemo

Page 15: Module 6 –Node.js and Sockettodd/cse330/cse330_lecture... · 1Extensible Networking Platform-CSE 330 –Creative Programming and Rapid Prototyping 1 Module 6 –Node.js and Socket.IO

Extensible Networking Platform 2929 - CSE 330 – Creative Programming and Rapid Prototyping

SimpleChatServerDemo