Event-driven IO server-side JavaScript environment based on V8 Engine
Upcoming SlideShare
Loading in...5
×
 

Event-driven IO server-side JavaScript environment based on V8 Engine

on

  • 4,444 views

Event-driven IO server-side JavaScript environment based on V8 Engine

Event-driven IO server-side JavaScript environment based on V8 Engine

Statistics

Views

Total Views
4,444
Views on SlideShare
4,400
Embed Views
44

Actions

Likes
4
Downloads
78
Comments
0

1 Embed 44

http://blog.ricardosilva.pt 44

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Event-driven IO server-side JavaScript environment based on V8 Engine Event-driven IO server-side JavaScript environment based on V8 Engine Presentation Transcript

  • Ricardo Silva Degree in Computer Science @ ISTECMSc Computation and Medical Instrumentation @ ISEP Software Developer @ Shortcut, Lda (blog): http://blog.ricardosilva.pt (email): nodejs@ricardosilva.pt
  • Event-driven I/O server-side JavaScriptenvironment based on V8 Engine
  • JavaScript(examples)
  • JavaScript: Array vs Object Instantiation• Arrayvar ar = []; /* ou new Array(); */• Objectvar carColors = {}; /* ou new Object();*/
  • JavaScript: Array vs Object Add• Arrayvar ar = [];ar[0] = January;Ouar.push(‘March’);• Objectvar carColors = {};carColors[BMW] = Red;carColors[Audi] = Blue;
  • JavaScript: Array vs Object Remove• Arrayar.splice(0,1);• Objectdelete carColors[BMW];
  • JavaScript: Array vs Object Lookup• Arrayvar ar = [];for (var i = 0; i < carColors.length; i++) { alert( ar[i] );};Ouvar month = ar[0];• Objectvar carColors = {};for (var i in carColors) { alert( carColors[i] );};Ouvar colorOfBWM = carColors[‘BMW’];
  • JavaScript: callback Functionsvar state;function setstate (pbool, difCallBack) { if (state != pbool) return (state = pbool), difCallBack(state);}setstate(true, function(s){ alert(s);}); “We can keep on doing other things while waiting for the callback to be called.”
  • JavaScript: Eventsvar eventHandlers = new Object(); this.registerEvent=function(name,callback){ var eh=(eventHandlers[name])?eventHandlers[name]:(eventHandlers[name]=[]); eh[eh.length]=callback; }; this.unregisterEvent=function(name,callback){ var eh=eventHandlers[name]; if("object"==typeof eh) for(var h in eh) if(eh[h]===callback){ delete eh[h]; return; } };
  • JavaScript: Eventsthis.triggerEvent=function(name,args){ var eh=eventHandlers[name]; if("object"==typeof eh) for(var h in eh) eh[h](args); }; this.ring=function(evDelegate){ xpto.registerEvent(ring,evDelegate); }; function onRing(args){ xpto.triggerEvent(ring,args); }
  • Node.js
  • Node.js: What is?“ Node.js is a platform built on Chromes JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices. ”
  • Node.js: Companies using node!• LinkedIn(Mobile WebApp) • Yahoo!Mail• Ebay(Data retrieval gateway) • Rackspace(Cloud kick monitoring)• GitHub(for Downloads) • Voxxer(Push to Talk mobile app)• Palm/HP(in WebOS)
  • Node.js: How to install?• $ git clone git://github.com/joyent/node.git• $ cd node• $ git checkout v0.6.0• $ ./configure• $ sudo make install (windows users: download node.exe)
  • Node.js DEMO
  • Node.js“ Come on…, server sideJS has been around since 1996 …”
  • Node.js“ …what is so special about node? ”
  • Node.js: Seep Speed• Node can do ~6000 http requests / sec per CPU core• It is no problem to handle thousands of concurrent connections which are moderately active
  • Node.js: V8 JS Engine V8 JavaScript Engine• Developed by Google in Aarhus, Denmark for Google Chrome• Translates JavaScript in Assembly Code• Crankshaft JIT (enabled in 0.6.0 by default)
  • Node.js: Non-Blocking I/O Non-Blocking I/OBlocking:var fs = require(fs);var one = fs.readFileSync(one.txt,utf-8);console.log(Read file one);var two = fs.readFileSync(two.txt,utf-8);console.log(Read file two);Non-Blocking:var fs = require(fs);fs.readFile(one.txt,utf-8,function(err,data) { console.log(Read file one);});fs.readFile(two.txt,utf-8,function(err,data) { console.log(Read file two);}); DEMO
  • Node.js: Non-Blocking I/O Blocking I/ORead one.txt (20ms) Read two.txt (10ms)Total duration (30ms)
  • Node.js: Non-Blocking I/O Non-Blocking I/ORead one.txt (20ms)Read two.txt (10ms)Total duration (20ms)
  • Node.js: Non-Blocking I/O Non-Blocking I/O• Close to ideal for high concurrency / high through put, single execution stack• Forces you to write more efficient code by parallelizing your I/O• Feels pretty much like AJAX in the browser
  • Node - Modules DEMO
  • Websockets
  • Websockets: What is?“The WebSocket specification—developed as part of the HTML5initiative—introduced the WebSocket JavaScript interface, whichdefines a full-duplex single socket connection over whichmessages can be sent between client and server. The WebSocketstandard simplifies much of the complexity around bi-directional web communication and connection management.WebSocket represents the next evolutionary step in webcommunication compared to Comet and Ajax. However, eachtechnology has its own unique capabilities. Learn how thesetechnologies vary so you can make the right choice.”
  • Websockets: What is?• Persistent connection between browser / server• The solution of the problem with RT at browser sidevar ws = new WebSocket(url);ws.onopen = function() { // When the connection opens};ws.onmessage = function() { // When the server sends data};ws.onclose = function() { // When the connection is closed};ws.send (Hi all!);ws.close();
  • Websockets: Compatibility
  • Websockets: Compatibility• We can use different transports:• HTML5 WebSockets• Flash Socket• AJAX Long Polling• Forever Iframe
  • Socket.io
  • Socket.io“ Socket.io aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanism. It’s care-free realtime 100% in JavaScript.”
  • Socket.io: transports?• Web Socket• Flash Socket• HTML File• XHR Polling• JSONP Polling
  • Socket.io: How to install? npm install socket.io
  • Socket.io DEMO
  • Socket.io: Other methods?//broadcast a message to all socketssocket.broadcast.emit(event);//no need to internally buffer this msgsocket.volatile.emit(event);//broadcast, nut only to people in this roomsocket.broadcast.to(room).emit(event);//broadcast to everyoneio.sockets.emit(event);io.sockets.send(hello);
  • Questions? (blog): http://blog.ricardosilva.pt (email): nodejs@ricardosilva.pt