SlideShare ist ein Scribd-Unternehmen logo
1 von 30
Node.js
Threads – Modules - Callbacks
2
Threads versus Events
request = readRequest(socket);
reply = processRequest(request);
sendReply(socket, reply);
Implementation:
- Thread switching and scheduler
readRequest(socket, function(request) {
processRequest(request,
function (reply) {
sendReply(socket, reply);
});
});
Implementation:
- Event queue processing
3
Event queue
while (true) {
if (!eventQueue.notEmpty()) {
eventQueue.pop().call();
}
}
Inner loop
1. launchReadRequest(socket); // Returns immediately
2. eventQueue.push(readDoneEventHandler); // when readRequest is complete
Never wait/block in event handler [ex: readRequest(socket);]
4
Node.js
● Takes JavaScript engine from a browser (Chrome's V8 JavaScript Engine)
○ Get same JavaScript on both browser and server
○ Don't need the DOM on the server
● Adds events and an event queue
○ Everything runs as a call from the event loop
● Makes event interface to all OS operations
○ Wrap all the OS blocking calls (file and socket/network io)
○ Add some data handle support
● Adds a proper module system
○ Each module gets its own scope (not everything in document.window)
5
Example: Node.js callback
var fs = require("fs"); // require is a Node module call
// fs object wraps OS sync file system calls
// OS read() is synchronous but Node's fs.readFile is asynchronous
fs.readFile("smallFile", readDoneCallback); // Start read
function readDoneCallback(error, dataBuffer) {
// By convention: First argument is JavaScript Error object
// dataBuffer is a special Node Buffer object
if (!error) {
console.log("smallFile contents", dataBuffer.toString());
}
}
6
Node Modules
var notGlobal;
function func1() {}
function func2() {}
module.exports = {func1: func1, func2: func2};
● Import using require()
○ System module: require("fs"); // Looks in node_module directories
○ From a file: require("./XXX.js"); // Reads specified file
○ From a directory: require("./myModule"); // Reads myModule/index.js
● Module files have a private scope
○ Can declare variables that would be global in the browser
○ Require returns what is assigned to module.exports
7
Node modules
● Many standard Node modules
○ File system, process access, networking, timers, devices, crypto, etc.
● Huge library of modules (npm)
○ Do pretty much anything you want
● We use:
○ Express - Fast, unopinionated, minimalist web framework (speak HTTP)
○ Mongoose - Elegant mongodb object modeling (speak to the database)
8
Node Buffer class
● Manipulating lots of binary data wasn't a strength of the JavaScript engine
○ Of course, that is what web servers do:
DBMS ⇔ Web Server ⇔ Browser
● Node added a Buffer class - Optimized for storing and operating on
binary data
○ Interface looks an array of bytes (like the OS system calls use)
○ Memory is allocated outside of the V8 heap
● Used by the “wrapped” OS I/O calls (fs, net, …)
● Thus optimizing sharing with pointers rather than always copying
○ buffer.copy()
○ For example: socket.write instead of fs.readFile
9
Buffer operations
● Supports operations for selecting or updating certain values
○ Can peek at some values and send the bulk of it on its way
● Can convert a buffer or parts of a buffer to a JavaScript string
○ buf.toString("utf8"); // Convert to UTF8 - commonly used on the web
○ buf.toString("hex"); // Convert to hex encoding (2 digits per byte)
○ buf.toString("base64"); // Convert to base64 encoding
10
Example: Node.js callbacks (redux)
var fs = require("fs");
// fs has 81 properties: readFile, writeFile, most OS calls, etc.
fs.readFile("smallFile", readDoneCallback);
// Read has been launched - JavaScript execution continues
// Node.js exits when no callbacks are outstanding
function readDoneCallback(error, dataBuffer) {
// console.log(dataBuffer) prints <Buffer 66 73 20 3d 20 72 65 71 ...
if (!error) {
console.log("smallFile contents", dataBuffer.toString());
}
}
11
Programming with Events/Callbacks
r1 = step1();
console.log('step1 done', r1);
r2 = step2(r1);
console.log('step2 done', r2);
r3 = step3(r2);
console.log('step3 done', r3);
console.log('All Done!');
● Threads: Blocking/waiting is transparent
● Events: Blocking/waiting requires callback
step1(function(r1) {
console.log('step1 done', r1);
step2(r1, function (r2) {
console.log('step2 done', r2);
step3(r2, function (r3) {
console.log('step3 done',r3);
});
});
}):
THREADS EVENTS
12
Programming with Events/Callbacks
● Key difference
○ Threads: Blocking/waiting is transparent
○ Events: Blocking/waiting requires callback
● Mental model
○ If code doesn't block: Same as thread programming
○ If code does block (or needs to block): Need to setup callback
○ Often what was a return statement becomes a function call
13
Example: Three step process(broken)
step1(function(r1) {
console.log('step1 done', r1);
step2(r1, function (r2) {
console.log('step2 done', r2);
step3(r2, function (r3) {
console.log('step3 done',r3);
});
});
}):
console.log('All Done!');
// Won't work! Call outside scope!
THREADS (or non-blocking) CALLBACKS
r1 = step1();
console.log('step1 done', r1);
r2 = step2(r1);
console.log('step2 done', r2);
r3 = step3(r2);
console.log('step3 done', r3);
console.log('All Done!');
14
Example: Three-step process(repaired)
step1(function(r1) {
console.log('step1 done', r1);
step2(r1, function (r2) {
console.log('step2 done', r2);
step3(r2, function (r3) {
console.log('step3 done',r3);
console.log('All Done!');
});
});
}):
EVENTS
r1 = step1();
console.log('step1 done', r1);
r2 = step2(r1);
console.log('step2 done', r2);
r3 = step3(r2);
console.log('step3 done', r3);
console.log('All Done!');
THREADS
15
Listener/emitter pattern
● When programing with events (rather than threads) a
listener/emitter pattern is used.
● Listener - Function to be called when the event is signaled
○ Should be familiar from DOM programming
(addEventListerner)
● Emitter - Signal that an event has occurred
○ Emit an event cause all the listener functions to be called
16
Node: EventEmitter = require('events');
myEmitter.on('myEvent', function(param1, param2) {
console.log('myEvent occurred with ' + param1 + ' and ' + param2 + '!');
});
myEmitter.emit('myEvent', 'arg1', 'arg2');
● On emit call listeners are called synchronously and in the
order the listeners were registered
● If no listener, then emit() is non-operative
● Listen with on() and signal with emit()
17
Typical EventEmitter patterns
myEmitter.on('conditionA', doConditionA);
myEmitter.on('conditionB', doConditionB);
myEmitter.on('conditionC', doConditionC);
myEmitter.on('error', handleErrorCondition);
● Handling 'error' exceptions is important - Node exits if
they are not caught!
● Have multiple different events for different state or actions
myEmitter.emit('error', new Error('Ouch!'));
18
Streams
● Build modules that produce and/or consume streams of data
● A popular way of structuring servers
○ Network socket ⇔ TCP/IP protocol processing ⇔ HTTP protocol processing ⇔ your code
● Can build connected streams dynamically
○ Add modules on stream: E.g. stream.push(Encryption)
Network socket ⇔ TCP/IP protocol processing ⇔ Encryption ⇔ HTTP processing
● Node's APIs heavily uses streams
○ Readable streams (e.g. fs.createReadStream)
○ Writable stream (e.g. fs.createWriteStream)
○ Duplex stream (e.g. net.createConnection)
○ Transform stream (e.g. zlib, crypto)
19
Readable streams - File reading using streams
var readableStreamEvent = fs.createReadStream("smallFile");
readableStreamEvent.on('data', function (chunkBuffer) {
// Could be called multiple times
console.log('got chunk of', chunkBuffer.length, 'bytes');
});
readableStreamEvent.on('end', function() {
// Called after all chunks read
console.log('got all the data');
});
readableStreamEvent.on('error', function (err) {
console.error('got error', err);
});
20
Writable streams - File writing using streams
var writableStreamEvent = fs.createWriteStream('outputFile');
writableStreamEvent.on('finish', function () {
console.log('file has been written!');
});
writableStreamEvent.write('Hello world!n');
writableStreamEvent.end();
21
Socket setup for TCP connections
sock = socket(AF_INET, SOCK_STREAM,
0);
connect(sock, &serverAddr,
sizeof(serverAddr));
write(sock, "Hi!", 3);
read(sock, buf, 3)
Server (Web Server)
lfd = socket(AF_INET, SOCK_STREAM, 0);
bind(lfd, &serverSddr, sizeof(serveraddr));
listen(lfd, 5);
sock = accept(lgf, &clientaddr, &clientlen);
read(sock, buf, 3);
write(sock, buf, 3)
Client (Browser)
● TCP/IP socket connection is a reliable, in-order byte stream
○ Node: reads can return data in different chunks that sent
22
TCP Networking on Node.js
var net = require('net');
net.createServer(processTCPconnection).listen(4000);
// Creates a socket, binds port 4000, and listens for connections
// Calls function processTCPconnection on each TCP connection
● Node net module wraps OS's network routines
● Includes higher level functionality like:
23
Example: A chat server
var clients = []; // List of connected clients
function processTCPconnection(socket) {
clients.push(socket); // Add this client to our connected list
socket.on('data', function (data) {
broadcast( "> " + data, socket); // Send received data to all
});
socket.on('end', function () {
clients.splice(clients.indexOf(socket), 1); // remove socket
});
}
24
Chat Server: broadcast
// Send message to all clients
function broadcast(message, sender) {
clients.forEach(function (client) {
if (client === sender) return;
// Don't send it to sender
client.write(message);
});
}
● Our chat server implementation is done!
25
Putting it together: A real simple file server
net.createServer(function (socket) {
socket.on('data', function (fileName) {
fs.readFile(fileName.toString(), function (error, fileData) {
if (!error) {
socket.write(fileData); // Writing a Buffer
} else {
socket.write(error.message); // Writing a String
}
socket.end();
});
});
}).listen(4000);
26
Example: Read three files
read(open("f1"), buf1, f1Size);
read(open("f2"), buf2, f2Size);
read(open("f3"), buf3, f3Size);
LINUX NODE.JS
// known by some as "Callback
Hell"
fs.readFile("f1", function
(error, data1) {
fs.readFile("f2", function
(error, data2) {
fs.readFile("f3", function
(error, data3) {
});
});
});
27
Example: Read N files(broken)
var fileContents = {};
['f1','f2','f3'].forEach(function (fileName) {
fs.readFile(fileName, function (error, dataBuffer) {
assert(!error);
fileContents[fileName] = dataBuffer;
});
});
If we use fileContents, how do we
know when ALL reads are
completed?
Recall: Can't wait in NodeJS
28
Example: Read N files(fix)
var fileContents = {};
['f1','f2','f3'].forEach(function (fileName) {
fs.readFile(fileName, function (error, dataBuffer) {
assert(!error);
fileContents[fileName] = dataBuffer;
if (gotLastCallBack()) allDoneCallback(fileContents);
});
});
An 'inelegant' solution...
29
Example: Read N files(fix)
var fileContents = {};
async.each(['f1','f2','f3'], readIt, function (err) {
if (!err) console.log('Done'); // fileContents filled in
if (err) console.error('Got an error:', err.message);
});
function readIt(fileName, callback) {
fs.readFile(fileName, function (error, dataBuffer) {
fileContents[fileName] = dataBuffer;
callback(error);
});
}
● Solution: Write a function that turns waiting into a callback
● 'async' to the rescue!
30
Node.JS - many useful built-in modules
● Buffer
● C/C++ Addons
● Child Processes
● Cluster
● Console
● Crypto
● Debugger
● DNS
● Errors
● Events
● File System
● Globals
● Timers
● TLS/SSL
● TTY
● UDP/Datagram
● URL
● Utilities
● V8
● VM
● ZLIB
● HTTP
● HTTPS
● Modules
● Net
● OS
● Path
● Process
● Punycode
● Query Strings
● Readline
● REPL
● Stream
● String Decoder

Weitere ähnliche Inhalte

Was ist angesagt?

Killing any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented featureKilling any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented featureCyber Security Alliance
 
Hibernate Import.Sql I18n
Hibernate Import.Sql I18nHibernate Import.Sql I18n
Hibernate Import.Sql I18nyifi2009
 
How to make a large C++-code base manageable
How to make a large C++-code base manageableHow to make a large C++-code base manageable
How to make a large C++-code base manageablecorehard_by
 
Pf: the OpenBSD packet filter
Pf: the OpenBSD packet filterPf: the OpenBSD packet filter
Pf: the OpenBSD packet filterGiovanni Bechis
 
Other Approaches (Concurrency)
Other Approaches (Concurrency)Other Approaches (Concurrency)
Other Approaches (Concurrency)Sri Prasanna
 
Node.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterNode.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterSimen Li
 
OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由Orangle Liu
 
Memory Management of C# with Unity Native Collections
Memory Management of C# with Unity Native CollectionsMemory Management of C# with Unity Native Collections
Memory Management of C# with Unity Native CollectionsYoshifumi Kawai
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Giovanni Bechis
 
Flowchart - Building next gen malware behavioural analysis environment
Flowchart - Building next gen malware behavioural analysis environment Flowchart - Building next gen malware behavioural analysis environment
Flowchart - Building next gen malware behavioural analysis environment isc2-hellenic
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.UA Mobile
 
Implementações paralelas
Implementações paralelasImplementações paralelas
Implementações paralelasWillian Molinari
 
Ceph OSD Op trace
Ceph OSD Op traceCeph OSD Op trace
Ceph OSD Op trace畅 刘
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Alexey Fyodorov
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingSteve Rhoades
 

Was ist angesagt? (20)

Killing any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented featureKilling any security product … using a Mimikatz undocumented feature
Killing any security product … using a Mimikatz undocumented feature
 
Hibernate Import.Sql I18n
Hibernate Import.Sql I18nHibernate Import.Sql I18n
Hibernate Import.Sql I18n
 
How to make a large C++-code base manageable
How to make a large C++-code base manageableHow to make a large C++-code base manageable
How to make a large C++-code base manageable
 
Pf: the OpenBSD packet filter
Pf: the OpenBSD packet filterPf: the OpenBSD packet filter
Pf: the OpenBSD packet filter
 
Other Approaches (Concurrency)
Other Approaches (Concurrency)Other Approaches (Concurrency)
Other Approaches (Concurrency)
 
Pledge in OpenBSD
Pledge in OpenBSDPledge in OpenBSD
Pledge in OpenBSD
 
Node.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitterNode.js Event Loop & EventEmitter
Node.js Event Loop & EventEmitter
 
OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由OpenResty TCP 服务代理和动态路由
OpenResty TCP 服务代理和动态路由
 
Osol Pgsql
Osol PgsqlOsol Pgsql
Osol Pgsql
 
Memory Management of C# with Unity Native Collections
Memory Management of C# with Unity Native CollectionsMemory Management of C# with Unity Native Collections
Memory Management of C# with Unity Native Collections
 
Gevent rabbit rpc
Gevent rabbit rpcGevent rabbit rpc
Gevent rabbit rpc
 
Anyevent
AnyeventAnyevent
Anyevent
 
Cooking pies with Celery
Cooking pies with CeleryCooking pies with Celery
Cooking pies with Celery
 
Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)Linux seccomp(2) vs OpenBSD pledge(2)
Linux seccomp(2) vs OpenBSD pledge(2)
 
Flowchart - Building next gen malware behavioural analysis environment
Flowchart - Building next gen malware behavioural analysis environment Flowchart - Building next gen malware behavioural analysis environment
Flowchart - Building next gen malware behavioural analysis environment
 
Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.Coroutines in Kotlin. UA Mobile 2017.
Coroutines in Kotlin. UA Mobile 2017.
 
Implementações paralelas
Implementações paralelasImplementações paralelas
Implementações paralelas
 
Ceph OSD Op trace
Ceph OSD Op traceCeph OSD Op trace
Ceph OSD Op trace
 
Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)Counter Wars (JEEConf 2016)
Counter Wars (JEEConf 2016)
 
Asynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time MessagingAsynchronous PHP and Real-time Messaging
Asynchronous PHP and Real-time Messaging
 

Ähnlich wie Node js lecture

NodeJSnodesforfreeinmyworldgipsnndnnd.pdf
NodeJSnodesforfreeinmyworldgipsnndnnd.pdfNodeJSnodesforfreeinmyworldgipsnndnnd.pdf
NodeJSnodesforfreeinmyworldgipsnndnnd.pdfVivekSonawane45
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsOhad Kravchick
 
Multi-core Node.pdf
Multi-core Node.pdfMulti-core Node.pdf
Multi-core Node.pdfAhmed Hassan
 
How NOT to write in Node.js
How NOT to write in Node.jsHow NOT to write in Node.js
How NOT to write in Node.jsPiotr Pelczar
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overviewjessesanford
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSAdam L Barrett
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the wayOleg Podsechin
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.Mike Brevoort
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applicationsTom Croucher
 
New kid on the block node.js
New kid on the block node.jsNew kid on the block node.js
New kid on the block node.jsJoel Divekar
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
Scalable Socket Server by Aryo
Scalable Socket Server by AryoScalable Socket Server by Aryo
Scalable Socket Server by AryoAgate Studio
 
Node js
Node jsNode js
Node jshazzaz
 

Ähnlich wie Node js lecture (20)

NodeJSnodesforfreeinmyworldgipsnndnnd.pdf
NodeJSnodesforfreeinmyworldgipsnndnnd.pdfNodeJSnodesforfreeinmyworldgipsnndnnd.pdf
NodeJSnodesforfreeinmyworldgipsnndnnd.pdf
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Building and Scaling Node.js Applications
Building and Scaling Node.js ApplicationsBuilding and Scaling Node.js Applications
Building and Scaling Node.js Applications
 
Multi-core Node.pdf
Multi-core Node.pdfMulti-core Node.pdf
Multi-core Node.pdf
 
How NOT to write in Node.js
How NOT to write in Node.jsHow NOT to write in Node.js
How NOT to write in Node.js
 
Dragoncraft Architectural Overview
Dragoncraft Architectural OverviewDragoncraft Architectural Overview
Dragoncraft Architectural Overview
 
soft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.jssoft-shake.ch - Hands on Node.js
soft-shake.ch - Hands on Node.js
 
Think Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJSThink Async: Asynchronous Patterns in NodeJS
Think Async: Asynchronous Patterns in NodeJS
 
Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
NodeJS for Beginner
NodeJS for BeginnerNodeJS for Beginner
NodeJS for Beginner
 
Node.js - async for the rest of us.
Node.js - async for the rest of us.Node.js - async for the rest of us.
Node.js - async for the rest of us.
 
Event driven programming -- Node.JS
Event driven programming -- Node.JSEvent driven programming -- Node.JS
Event driven programming -- Node.JS
 
Writing robust Node.js applications
Writing robust Node.js applicationsWriting robust Node.js applications
Writing robust Node.js applications
 
New kid on the block node.js
New kid on the block node.jsNew kid on the block node.js
New kid on the block node.js
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
Scalable Socket Server by Aryo
Scalable Socket Server by AryoScalable Socket Server by Aryo
Scalable Socket Server by Aryo
 
Book
BookBook
Book
 
Node js
Node jsNode js
Node js
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 

Mehr von Darryl Sherman

Java Naming & Directory Services
Java Naming & Directory ServicesJava Naming & Directory Services
Java Naming & Directory ServicesDarryl Sherman
 
Il 02-search requeststrings
Il 02-search requeststringsIl 02-search requeststrings
Il 02-search requeststringsDarryl Sherman
 
PSD to HTML Conversion
PSD to HTML ConversionPSD to HTML Conversion
PSD to HTML ConversionDarryl Sherman
 
Angular js filters and directives
Angular js filters and directivesAngular js filters and directives
Angular js filters and directivesDarryl Sherman
 

Mehr von Darryl Sherman (6)

Java Naming & Directory Services
Java Naming & Directory ServicesJava Naming & Directory Services
Java Naming & Directory Services
 
Il 01-search engines
Il 01-search enginesIl 01-search engines
Il 01-search engines
 
Il 02-search requeststrings
Il 02-search requeststringsIl 02-search requeststrings
Il 02-search requeststrings
 
Python unit testing
Python unit testingPython unit testing
Python unit testing
 
PSD to HTML Conversion
PSD to HTML ConversionPSD to HTML Conversion
PSD to HTML Conversion
 
Angular js filters and directives
Angular js filters and directivesAngular js filters and directives
Angular js filters and directives
 

Kürzlich hochgeladen

SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profileakrivarotava
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfkalichargn70th171
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesKrzysztofKkol1
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Anthony Dahanne
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfRTS corp
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptxVinzoCenzo
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...OnePlan Solutions
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxRTS corp
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesVictoriaMetrics
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 

Kürzlich hochgeladen (20)

SoftTeco - Software Development Company Profile
SoftTeco - Software Development Company ProfileSoftTeco - Software Development Company Profile
SoftTeco - Software Development Company Profile
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdfExploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
Exploring Selenium_Appium Frameworks for Seamless Integration with HeadSpin.pdf
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilitiesAmazon Bedrock in Action - presentation of the Bedrock's capabilities
Amazon Bedrock in Action - presentation of the Bedrock's capabilities
 
Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024Not a Kubernetes fan? The state of PaaS in 2024
Not a Kubernetes fan? The state of PaaS in 2024
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdfEnhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
Enhancing Supply Chain Visibility with Cargo Cloud Solutions.pdf
 
Osi security architecture in network.pptx
Osi security architecture in network.pptxOsi security architecture in network.pptx
Osi security architecture in network.pptx
 
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
Tech Tuesday - Mastering Time Management Unlock the Power of OnePlan's Timesh...
 
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptxThe Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
The Role of IoT and Sensor Technology in Cargo Cloud Solutions.pptx
 
What’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 UpdatesWhat’s New in VictoriaMetrics: Q1 2024 Updates
What’s New in VictoriaMetrics: Q1 2024 Updates
 
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News UpdateVictoriaMetrics Q1 Meet Up '24 - Community & News Update
VictoriaMetrics Q1 Meet Up '24 - Community & News Update
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 

Node js lecture

  • 2. 2 Threads versus Events request = readRequest(socket); reply = processRequest(request); sendReply(socket, reply); Implementation: - Thread switching and scheduler readRequest(socket, function(request) { processRequest(request, function (reply) { sendReply(socket, reply); }); }); Implementation: - Event queue processing
  • 3. 3 Event queue while (true) { if (!eventQueue.notEmpty()) { eventQueue.pop().call(); } } Inner loop 1. launchReadRequest(socket); // Returns immediately 2. eventQueue.push(readDoneEventHandler); // when readRequest is complete Never wait/block in event handler [ex: readRequest(socket);]
  • 4. 4 Node.js ● Takes JavaScript engine from a browser (Chrome's V8 JavaScript Engine) ○ Get same JavaScript on both browser and server ○ Don't need the DOM on the server ● Adds events and an event queue ○ Everything runs as a call from the event loop ● Makes event interface to all OS operations ○ Wrap all the OS blocking calls (file and socket/network io) ○ Add some data handle support ● Adds a proper module system ○ Each module gets its own scope (not everything in document.window)
  • 5. 5 Example: Node.js callback var fs = require("fs"); // require is a Node module call // fs object wraps OS sync file system calls // OS read() is synchronous but Node's fs.readFile is asynchronous fs.readFile("smallFile", readDoneCallback); // Start read function readDoneCallback(error, dataBuffer) { // By convention: First argument is JavaScript Error object // dataBuffer is a special Node Buffer object if (!error) { console.log("smallFile contents", dataBuffer.toString()); } }
  • 6. 6 Node Modules var notGlobal; function func1() {} function func2() {} module.exports = {func1: func1, func2: func2}; ● Import using require() ○ System module: require("fs"); // Looks in node_module directories ○ From a file: require("./XXX.js"); // Reads specified file ○ From a directory: require("./myModule"); // Reads myModule/index.js ● Module files have a private scope ○ Can declare variables that would be global in the browser ○ Require returns what is assigned to module.exports
  • 7. 7 Node modules ● Many standard Node modules ○ File system, process access, networking, timers, devices, crypto, etc. ● Huge library of modules (npm) ○ Do pretty much anything you want ● We use: ○ Express - Fast, unopinionated, minimalist web framework (speak HTTP) ○ Mongoose - Elegant mongodb object modeling (speak to the database)
  • 8. 8 Node Buffer class ● Manipulating lots of binary data wasn't a strength of the JavaScript engine ○ Of course, that is what web servers do: DBMS ⇔ Web Server ⇔ Browser ● Node added a Buffer class - Optimized for storing and operating on binary data ○ Interface looks an array of bytes (like the OS system calls use) ○ Memory is allocated outside of the V8 heap ● Used by the “wrapped” OS I/O calls (fs, net, …) ● Thus optimizing sharing with pointers rather than always copying ○ buffer.copy() ○ For example: socket.write instead of fs.readFile
  • 9. 9 Buffer operations ● Supports operations for selecting or updating certain values ○ Can peek at some values and send the bulk of it on its way ● Can convert a buffer or parts of a buffer to a JavaScript string ○ buf.toString("utf8"); // Convert to UTF8 - commonly used on the web ○ buf.toString("hex"); // Convert to hex encoding (2 digits per byte) ○ buf.toString("base64"); // Convert to base64 encoding
  • 10. 10 Example: Node.js callbacks (redux) var fs = require("fs"); // fs has 81 properties: readFile, writeFile, most OS calls, etc. fs.readFile("smallFile", readDoneCallback); // Read has been launched - JavaScript execution continues // Node.js exits when no callbacks are outstanding function readDoneCallback(error, dataBuffer) { // console.log(dataBuffer) prints <Buffer 66 73 20 3d 20 72 65 71 ... if (!error) { console.log("smallFile contents", dataBuffer.toString()); } }
  • 11. 11 Programming with Events/Callbacks r1 = step1(); console.log('step1 done', r1); r2 = step2(r1); console.log('step2 done', r2); r3 = step3(r2); console.log('step3 done', r3); console.log('All Done!'); ● Threads: Blocking/waiting is transparent ● Events: Blocking/waiting requires callback step1(function(r1) { console.log('step1 done', r1); step2(r1, function (r2) { console.log('step2 done', r2); step3(r2, function (r3) { console.log('step3 done',r3); }); }); }): THREADS EVENTS
  • 12. 12 Programming with Events/Callbacks ● Key difference ○ Threads: Blocking/waiting is transparent ○ Events: Blocking/waiting requires callback ● Mental model ○ If code doesn't block: Same as thread programming ○ If code does block (or needs to block): Need to setup callback ○ Often what was a return statement becomes a function call
  • 13. 13 Example: Three step process(broken) step1(function(r1) { console.log('step1 done', r1); step2(r1, function (r2) { console.log('step2 done', r2); step3(r2, function (r3) { console.log('step3 done',r3); }); }); }): console.log('All Done!'); // Won't work! Call outside scope! THREADS (or non-blocking) CALLBACKS r1 = step1(); console.log('step1 done', r1); r2 = step2(r1); console.log('step2 done', r2); r3 = step3(r2); console.log('step3 done', r3); console.log('All Done!');
  • 14. 14 Example: Three-step process(repaired) step1(function(r1) { console.log('step1 done', r1); step2(r1, function (r2) { console.log('step2 done', r2); step3(r2, function (r3) { console.log('step3 done',r3); console.log('All Done!'); }); }); }): EVENTS r1 = step1(); console.log('step1 done', r1); r2 = step2(r1); console.log('step2 done', r2); r3 = step3(r2); console.log('step3 done', r3); console.log('All Done!'); THREADS
  • 15. 15 Listener/emitter pattern ● When programing with events (rather than threads) a listener/emitter pattern is used. ● Listener - Function to be called when the event is signaled ○ Should be familiar from DOM programming (addEventListerner) ● Emitter - Signal that an event has occurred ○ Emit an event cause all the listener functions to be called
  • 16. 16 Node: EventEmitter = require('events'); myEmitter.on('myEvent', function(param1, param2) { console.log('myEvent occurred with ' + param1 + ' and ' + param2 + '!'); }); myEmitter.emit('myEvent', 'arg1', 'arg2'); ● On emit call listeners are called synchronously and in the order the listeners were registered ● If no listener, then emit() is non-operative ● Listen with on() and signal with emit()
  • 17. 17 Typical EventEmitter patterns myEmitter.on('conditionA', doConditionA); myEmitter.on('conditionB', doConditionB); myEmitter.on('conditionC', doConditionC); myEmitter.on('error', handleErrorCondition); ● Handling 'error' exceptions is important - Node exits if they are not caught! ● Have multiple different events for different state or actions myEmitter.emit('error', new Error('Ouch!'));
  • 18. 18 Streams ● Build modules that produce and/or consume streams of data ● A popular way of structuring servers ○ Network socket ⇔ TCP/IP protocol processing ⇔ HTTP protocol processing ⇔ your code ● Can build connected streams dynamically ○ Add modules on stream: E.g. stream.push(Encryption) Network socket ⇔ TCP/IP protocol processing ⇔ Encryption ⇔ HTTP processing ● Node's APIs heavily uses streams ○ Readable streams (e.g. fs.createReadStream) ○ Writable stream (e.g. fs.createWriteStream) ○ Duplex stream (e.g. net.createConnection) ○ Transform stream (e.g. zlib, crypto)
  • 19. 19 Readable streams - File reading using streams var readableStreamEvent = fs.createReadStream("smallFile"); readableStreamEvent.on('data', function (chunkBuffer) { // Could be called multiple times console.log('got chunk of', chunkBuffer.length, 'bytes'); }); readableStreamEvent.on('end', function() { // Called after all chunks read console.log('got all the data'); }); readableStreamEvent.on('error', function (err) { console.error('got error', err); });
  • 20. 20 Writable streams - File writing using streams var writableStreamEvent = fs.createWriteStream('outputFile'); writableStreamEvent.on('finish', function () { console.log('file has been written!'); }); writableStreamEvent.write('Hello world!n'); writableStreamEvent.end();
  • 21. 21 Socket setup for TCP connections sock = socket(AF_INET, SOCK_STREAM, 0); connect(sock, &serverAddr, sizeof(serverAddr)); write(sock, "Hi!", 3); read(sock, buf, 3) Server (Web Server) lfd = socket(AF_INET, SOCK_STREAM, 0); bind(lfd, &serverSddr, sizeof(serveraddr)); listen(lfd, 5); sock = accept(lgf, &clientaddr, &clientlen); read(sock, buf, 3); write(sock, buf, 3) Client (Browser) ● TCP/IP socket connection is a reliable, in-order byte stream ○ Node: reads can return data in different chunks that sent
  • 22. 22 TCP Networking on Node.js var net = require('net'); net.createServer(processTCPconnection).listen(4000); // Creates a socket, binds port 4000, and listens for connections // Calls function processTCPconnection on each TCP connection ● Node net module wraps OS's network routines ● Includes higher level functionality like:
  • 23. 23 Example: A chat server var clients = []; // List of connected clients function processTCPconnection(socket) { clients.push(socket); // Add this client to our connected list socket.on('data', function (data) { broadcast( "> " + data, socket); // Send received data to all }); socket.on('end', function () { clients.splice(clients.indexOf(socket), 1); // remove socket }); }
  • 24. 24 Chat Server: broadcast // Send message to all clients function broadcast(message, sender) { clients.forEach(function (client) { if (client === sender) return; // Don't send it to sender client.write(message); }); } ● Our chat server implementation is done!
  • 25. 25 Putting it together: A real simple file server net.createServer(function (socket) { socket.on('data', function (fileName) { fs.readFile(fileName.toString(), function (error, fileData) { if (!error) { socket.write(fileData); // Writing a Buffer } else { socket.write(error.message); // Writing a String } socket.end(); }); }); }).listen(4000);
  • 26. 26 Example: Read three files read(open("f1"), buf1, f1Size); read(open("f2"), buf2, f2Size); read(open("f3"), buf3, f3Size); LINUX NODE.JS // known by some as "Callback Hell" fs.readFile("f1", function (error, data1) { fs.readFile("f2", function (error, data2) { fs.readFile("f3", function (error, data3) { }); }); });
  • 27. 27 Example: Read N files(broken) var fileContents = {}; ['f1','f2','f3'].forEach(function (fileName) { fs.readFile(fileName, function (error, dataBuffer) { assert(!error); fileContents[fileName] = dataBuffer; }); }); If we use fileContents, how do we know when ALL reads are completed? Recall: Can't wait in NodeJS
  • 28. 28 Example: Read N files(fix) var fileContents = {}; ['f1','f2','f3'].forEach(function (fileName) { fs.readFile(fileName, function (error, dataBuffer) { assert(!error); fileContents[fileName] = dataBuffer; if (gotLastCallBack()) allDoneCallback(fileContents); }); }); An 'inelegant' solution...
  • 29. 29 Example: Read N files(fix) var fileContents = {}; async.each(['f1','f2','f3'], readIt, function (err) { if (!err) console.log('Done'); // fileContents filled in if (err) console.error('Got an error:', err.message); }); function readIt(fileName, callback) { fs.readFile(fileName, function (error, dataBuffer) { fileContents[fileName] = dataBuffer; callback(error); }); } ● Solution: Write a function that turns waiting into a callback ● 'async' to the rescue!
  • 30. 30 Node.JS - many useful built-in modules ● Buffer ● C/C++ Addons ● Child Processes ● Cluster ● Console ● Crypto ● Debugger ● DNS ● Errors ● Events ● File System ● Globals ● Timers ● TLS/SSL ● TTY ● UDP/Datagram ● URL ● Utilities ● V8 ● VM ● ZLIB ● HTTP ● HTTPS ● Modules ● Net ● OS ● Path ● Process ● Punycode ● Query Strings ● Readline ● REPL ● Stream ● String Decoder