SlideShare una empresa de Scribd logo
1 de 51
Descargar para leer sin conexión
Node.js: Patterns and Opinions
Isaac Z. Schlueter – TxJS 2013
Monday, April 15, 13
SUPER FAST
NODE INTROomg!
Monday, April 15, 13
WTF is a Node.js?
•JavaScript on the Server
(or robots, helicopters, any other not-a-web-browser thing)
•Non-blocking I/O
•Callbacks, Events, and Streams
Monday, April 15, 13
conventional program
printLine('Enter your name:')
var name = getLine();
var r = db.save(name);
if (r === 'ok')
printLine('it worked!');
Monday, April 15, 13
conventional program
printLine('Enter your name:')
// waiting for I/O to happen!
var name = getLine();
// omg soooo slllloooowwwww
var r = db.save(name);
// taking literally forever
if (r === 'ok')
printLine('it worked!');
Monday, April 15, 13
callbacks!
printLine('Enter name:', function() {
getLine(function(err, name) {
db.save(name,function(err, r) {
if (r === 'ok')
printLine('it worked!');
});
});
});
Monday, April 15, 13
NOT ANY FASTER!
maybe a little bit slower, actually
Monday, April 15, 13
Non-blocking I/O is not about
making I/O faster
•It's about making I/O not prevent other I/O
•Doing things in this way adds a slight amount of overhead.
•However, we're not monopolizing the process while we wait.
•Our server can be handling other requests while we're sitting
around doing nothing.
Monday, April 15, 13
Callback Drawbacks
•Stack gets unwound - Error().stack doesn't tell you much
•try/catch can't wrap throws effectively
•Boilerplate is hard to look at - `function` is a lot of characters
•Lots of indentation - "Christmas Tree Code"
Monday, April 15, 13
THREADS
Monday, April 15, 13
Node is
"single-threaded"
Monday, April 15, 13
Node actually has a lot of
internal threads, but...
•Only one thread is running JavaScript
•That means that your JS will not have to worry about
some other JS getting in its way
•Simplifies parallel coding model dramatically
•Safe to share data between requests
•while(true); will prevent new requests
Monday, April 15, 13
COROUTINES
Monday, April 15, 13
Coroutines
•Cooperative Multitasking with yield and await keywords
•No preemption issues, same while(true); behavior
•Stack not unwound (try/throw/catch isn't broken)
•Not how JavaScript works (today)
•Requires modifying the semantics of the language
Monday, April 15, 13
LET'S HTTP!
Monday, April 15, 13
hello,TxJS
require('http').createServer(
function(req, res) {
res.end('hello, txjs!')
}
).listen(80)
Monday, April 15, 13
Callbacks for
EVERYTHING?
gotta be elfin kidding me...
Monday, April 15, 13
no, we're not jerks.
•callback: lowest Node abstraction for doing one async thing
•EventEmitter: for things that happen continuously over time
Monday, April 15, 13
cb vs ee
•callback: "Show me all the blueprints"
•EventEmitter: "Any time you get blueprints, show me them"
way of the future
Monday, April 15, 13
EventEmitter
var e = new EventEmitter();
e.on('foo', function(arg) {
console.log('baz' + arg)
});
e.emit('foo', 'bar');
// prints 'bazbar'
Monday, April 15, 13
hello, EventEmitter
var s = new http.Server()
s.on('request', function(req, res){
res.end('hello, txjs!')
})
s.listen(80)
Monday, April 15, 13
Streams
•When managing data flows, control throughput or ENOMEM
•Streams are a special EventEmitter that takes care of this
•inputGoober.pipe(outputThingie)
•When you grok this, you grok Node.
•(see also: Marco Rogers talk)
Monday, April 15, 13
OPINIONS
Monday, April 15, 13
JavaScript
Monday, April 15, 13
JavaScript
Monday, April 15, 13
Node is JavaScript
•Node is not a language - JavaScript is the language
•Portability, Events, Run to completion:The JavaScript way
•Not developing a language removes a huge burden.
Let TC-39 andV8 fight those battles for us!
•Node.js endeavors to embody the spirit of JavaScript,
not to change it.
Monday, April 15, 13
Errors must
be handled
Monday, April 15, 13
Do not ignore errors
•Callbacks all get an error object as their first argument
•EventEmitter 'error' event is magical... MAGICALLY DEADLY!
•Domains and process.on('uncaughtException') to clean up, but
continuing in error is almost always terrible!
•Exactly how to "not ignore" errors is a question of some
debate. Liberal (never crash) vs Conservative (crash asap)
Monday, April 15, 13
Monday, April 15, 13
Monday, April 15, 13
Portability++
Monday, April 15, 13
Increase Portability
•libuv: Evented nonblocking I/O for Computers
•If you write JavaScript, it'll run on every OS Node runs on
•Safety and portability are default.
•Sometimes portability is compromised for "When in Rome"
eg path strings:  on Windows, / on Unix
•If you write C++ addons, of course, all bets are off
Monday, April 15, 13
Slowness
is a Bug
Monday, April 15, 13
Slowness is a Bug
•Latency is far and away the most important metric
(Some would say, the only important metric!)
•Unnecessary slowness is a bug, a sign of incorrect behavior
•Necessary slowness must be isolated, reduced in scope
•A slow thing should not ever make other things slow
•This is what that nonblocking callback stuff is all about
node's http client breaks this rule badly right now. will be fixed in 0.12!
Monday, April 15, 13
Unix
Monday, April 15, 13
Unix Philosophy
•Interfaces should be obvious, simple, composible, consistent
•Callback => EventEmitter => Stream
•Portability, backwards compatibility
•"Every program is a proxy" - Ryan Dahl
•"Don't build big apps" - Substack
Monday, April 15, 13
Assemble
Monday, April 15, 13
Assemble rather than invent
•JavaScript is old
•Nonblocking I/O is ancient
•Unix is paleolithic
•Put proven things together in simple ways
•Unix philosophy applied to ideas
Monday, April 15, 13
Small Parts
Monday, April 15, 13
Small Core, Small Parts
•The scope of node's core API is much smaller than
comparable server runtimes
•No glob, no xml, etc. This is on purpose.
•Small core => active userland
•"The Standard Library is where modules go to die" - Python
•Most npm modules are tiny, single-purpose, composible
Monday, April 15, 13
Community
Monday, April 15, 13
Node is a Community
•Everyone is an npm publisher. Everyone is a collaborator.
•Minimum Necessary Process: Governance mostly laissez-faire
•Small core team, BDFL model; Expansive prolific community
•Alternatives are cherished! No "one true" anything
•Work together to explore the solution space.
Monday, April 15, 13
Monday, April 15, 13
Compassionate
Communities
Monday, April 15, 13
EGO
Monday, April 15, 13
JIFASNIF
Monday, April 15, 13
JIFASNIF
•JavaScript is fun, and so Node is fun.
•When making a decision, always choose the funner option
•"Fun" is a very non-trivial concept! Not "easy" or "simple" or
"complicated" or "featureful" or "hard"
•A feeling of efficacy, rising to challenges, moving fast
•It's fun to create, share, put parts together, help each other
•Reduce obstacles to having fun!
Monday, April 15, 13
FUTURE
Monday, April 15, 13
v0.12
•Massive cleanup of HTTP interfaces (especially client)
•General purpose socket pooling interface
•Faster TLS (especially initial handshake)
•Better Buffer memory management
Monday, April 15, 13
v1.0.0
•The next stable release after 0.12 (probably)
•Incremental improvements, uv/npm/V8 upgrades
•Not much in the way of new features
•Node-core's boundary is pretty much set
•The Node community's future is boundless
Monday, April 15, 13
See you on
the Internet!
http://j.mp/node-patterns-pdf
Monday, April 15, 13

Más contenido relacionado

La actualidad más candente

javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonDomingo Suarez Torres
 
10 Things you should know about Ruby
10 Things you should know about Ruby10 Things you should know about Ruby
10 Things you should know about Rubysikachu
 
Building MapAttack
Building MapAttackBuilding MapAttack
Building MapAttackKyle Drake
 
Next generation frontend tooling
Next generation frontend toolingNext generation frontend tooling
Next generation frontend toolingpksjce
 
Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Charles Nutter
 
JRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform FurtherJRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform FurtherCharles Nutter
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsOndrej Mihályi
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsOndrej Mihályi
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talkReuven Lerner
 
Test::Kantan - Perl and Testing
Test::Kantan - Perl and TestingTest::Kantan - Perl and Testing
Test::Kantan - Perl and TestingTokuhiro Matsuno
 
Steelcon 2015 - 0wning the internet of trash
Steelcon 2015 - 0wning the internet of trashSteelcon 2015 - 0wning the internet of trash
Steelcon 2015 - 0wning the internet of trashinfodox
 
Ruby is dying. What languages are cool now?
Ruby is dying. What languages are cool now?Ruby is dying. What languages are cool now?
Ruby is dying. What languages are cool now?Michał Konarski
 
Try harder or go home
Try harder or go homeTry harder or go home
Try harder or go homejaredhaight
 
BSides Hannover 2015 - Shell on Wheels
BSides Hannover 2015 - Shell on WheelsBSides Hannover 2015 - Shell on Wheels
BSides Hannover 2015 - Shell on Wheelsinfodox
 
The Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web DevelopmentThe Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web Developmenttwopoint718
 
Making Symfony Services async with RabbitMq (and more Symfony)
Making Symfony Services async with RabbitMq (and more Symfony)Making Symfony Services async with RabbitMq (and more Symfony)
Making Symfony Services async with RabbitMq (and more Symfony)Gaetano Giunta
 
Ruby projects of interest for DevOps
Ruby projects of interest for DevOpsRuby projects of interest for DevOps
Ruby projects of interest for DevOpsRicardo Sanchez
 

La actualidad más candente (20)

javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
 
10 Things you should know about Ruby
10 Things you should know about Ruby10 Things you should know about Ruby
10 Things you should know about Ruby
 
Building MapAttack
Building MapAttackBuilding MapAttack
Building MapAttack
 
Next generation frontend tooling
Next generation frontend toolingNext generation frontend tooling
Next generation frontend tooling
 
Scalable Web Apps
Scalable Web AppsScalable Web Apps
Scalable Web Apps
 
Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014Bringing Concurrency to Ruby - RubyConf India 2014
Bringing Concurrency to Ruby - RubyConf India 2014
 
JRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform FurtherJRuby: Pushing the Java Platform Further
JRuby: Pushing the Java Platform Further
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applications
 
Children of Ruby
Children of RubyChildren of Ruby
Children of Ruby
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applications
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talk
 
Test::Kantan - Perl and Testing
Test::Kantan - Perl and TestingTest::Kantan - Perl and Testing
Test::Kantan - Perl and Testing
 
Steelcon 2015 - 0wning the internet of trash
Steelcon 2015 - 0wning the internet of trashSteelcon 2015 - 0wning the internet of trash
Steelcon 2015 - 0wning the internet of trash
 
Ruby is dying. What languages are cool now?
Ruby is dying. What languages are cool now?Ruby is dying. What languages are cool now?
Ruby is dying. What languages are cool now?
 
Try harder or go home
Try harder or go homeTry harder or go home
Try harder or go home
 
BSides Hannover 2015 - Shell on Wheels
BSides Hannover 2015 - Shell on WheelsBSides Hannover 2015 - Shell on Wheels
BSides Hannover 2015 - Shell on Wheels
 
The Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web DevelopmentThe Transparent Web: Bridging the Chasm in Web Development
The Transparent Web: Bridging the Chasm in Web Development
 
Making Symfony Services async with RabbitMq (and more Symfony)
Making Symfony Services async with RabbitMq (and more Symfony)Making Symfony Services async with RabbitMq (and more Symfony)
Making Symfony Services async with RabbitMq (and more Symfony)
 
Ruby projects of interest for DevOps
Ruby projects of interest for DevOpsRuby projects of interest for DevOps
Ruby projects of interest for DevOps
 
Packers
PackersPackers
Packers
 

Similar a Node.js Patterns and Opinions

Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim SummitIntroduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim SummitRan Mizrahi
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.jsTroy Miles
 
Ignite@DevOpsDays - Why devs need ops
Ignite@DevOpsDays - Why devs need opsIgnite@DevOpsDays - Why devs need ops
Ignite@DevOpsDays - Why devs need opsMichael Brunton-Spall
 
Rust's Journey to Async/await
Rust's Journey to Async/awaitRust's Journey to Async/await
Rust's Journey to Async/awaitC4Media
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsKonrad Malawski
 
Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...
Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...
Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...Loadzen
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事Wen-Tien Chang
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscriptBill Buchan
 
Agile analytics applications on hadoop
Agile analytics applications on hadoopAgile analytics applications on hadoop
Agile analytics applications on hadoopRussell Jurney
 
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...Abraham Aranguren
 
When your code is nearly old enough to vote
When your code is nearly old enough to voteWhen your code is nearly old enough to vote
When your code is nearly old enough to votedreamwidth
 
How to Lose Functional Programming at Work
How to Lose Functional Programming at WorkHow to Lose Functional Programming at Work
How to Lose Functional Programming at WorkRobert Pearce
 
node.js in action
node.js in actionnode.js in action
node.js in actionKaran Misra
 
Things You Need to Know Before Starting An App-Openair2015 keynote
Things You Need to Know Before Starting An App-Openair2015 keynoteThings You Need to Know Before Starting An App-Openair2015 keynote
Things You Need to Know Before Starting An App-Openair2015 keynoteSana Nasar
 
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacDefcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacPriyanka Aash
 
Static-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptxStatic-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptxShivashankarHR1
 
Web Motion: Motion Detection on the Web
Web Motion: Motion Detection on the WebWeb Motion: Motion Detection on the Web
Web Motion: Motion Detection on the Webfisherwebdev
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA PresentationRob Tweed
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript TestingRan Mizrahi
 

Similar a Node.js Patterns and Opinions (20)

Introduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim SummitIntroduction to node.js by Ran Mizrahi @ Reversim Summit
Introduction to node.js by Ran Mizrahi @ Reversim Summit
 
Introduction to Node.js
Introduction to Node.jsIntroduction to Node.js
Introduction to Node.js
 
Ignite@DevOpsDays - Why devs need ops
Ignite@DevOpsDays - Why devs need opsIgnite@DevOpsDays - Why devs need ops
Ignite@DevOpsDays - Why devs need ops
 
Rust's Journey to Async/await
Rust's Journey to Async/awaitRust's Journey to Async/await
Rust's Journey to Async/await
 
Need for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applicationsNeed for Async: Hot pursuit for scalable applications
Need for Async: Hot pursuit for scalable applications
 
Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...
Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...
Load testing, Lessons learnt and Loadzen - Martin Buhr at DevTank - 31st Janu...
 
那些 Functional Programming 教我的事
那些 Functional Programming 教我的事那些 Functional Programming 教我的事
那些 Functional Programming 教我的事
 
Speed geeking-lotusscript
Speed geeking-lotusscriptSpeed geeking-lotusscript
Speed geeking-lotusscript
 
Agile analytics applications on hadoop
Agile analytics applications on hadoopAgile analytics applications on hadoop
Agile analytics applications on hadoop
 
Zero mq logs
Zero mq logsZero mq logs
Zero mq logs
 
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
Offensive (Web, etc) Testing Framework: My gift for the community - BerlinSid...
 
When your code is nearly old enough to vote
When your code is nearly old enough to voteWhen your code is nearly old enough to vote
When your code is nearly old enough to vote
 
How to Lose Functional Programming at Work
How to Lose Functional Programming at WorkHow to Lose Functional Programming at Work
How to Lose Functional Programming at Work
 
node.js in action
node.js in actionnode.js in action
node.js in action
 
Things You Need to Know Before Starting An App-Openair2015 keynote
Things You Need to Know Before Starting An App-Openair2015 keynoteThings You Need to Know Before Starting An App-Openair2015 keynote
Things You Need to Know Before Starting An App-Openair2015 keynote
 
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attacDefcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
Defcon 22-paul-mcmillan-attacking-the-iot-using-timing-attac
 
Static-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptxStatic-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptx
 
Web Motion: Motion Detection on the Web
Web Motion: Motion Detection on the WebWeb Motion: Motion Detection on the Web
Web Motion: Motion Detection on the Web
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript Testing
 

Último

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...chiefasafspells
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 

Último (20)

Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 

Node.js Patterns and Opinions

  • 1. Node.js: Patterns and Opinions Isaac Z. Schlueter – TxJS 2013 Monday, April 15, 13
  • 3. WTF is a Node.js? •JavaScript on the Server (or robots, helicopters, any other not-a-web-browser thing) •Non-blocking I/O •Callbacks, Events, and Streams Monday, April 15, 13
  • 4. conventional program printLine('Enter your name:') var name = getLine(); var r = db.save(name); if (r === 'ok') printLine('it worked!'); Monday, April 15, 13
  • 5. conventional program printLine('Enter your name:') // waiting for I/O to happen! var name = getLine(); // omg soooo slllloooowwwww var r = db.save(name); // taking literally forever if (r === 'ok') printLine('it worked!'); Monday, April 15, 13
  • 6. callbacks! printLine('Enter name:', function() { getLine(function(err, name) { db.save(name,function(err, r) { if (r === 'ok') printLine('it worked!'); }); }); }); Monday, April 15, 13
  • 7. NOT ANY FASTER! maybe a little bit slower, actually Monday, April 15, 13
  • 8. Non-blocking I/O is not about making I/O faster •It's about making I/O not prevent other I/O •Doing things in this way adds a slight amount of overhead. •However, we're not monopolizing the process while we wait. •Our server can be handling other requests while we're sitting around doing nothing. Monday, April 15, 13
  • 9. Callback Drawbacks •Stack gets unwound - Error().stack doesn't tell you much •try/catch can't wrap throws effectively •Boilerplate is hard to look at - `function` is a lot of characters •Lots of indentation - "Christmas Tree Code" Monday, April 15, 13
  • 12. Node actually has a lot of internal threads, but... •Only one thread is running JavaScript •That means that your JS will not have to worry about some other JS getting in its way •Simplifies parallel coding model dramatically •Safe to share data between requests •while(true); will prevent new requests Monday, April 15, 13
  • 14. Coroutines •Cooperative Multitasking with yield and await keywords •No preemption issues, same while(true); behavior •Stack not unwound (try/throw/catch isn't broken) •Not how JavaScript works (today) •Requires modifying the semantics of the language Monday, April 15, 13
  • 17. Callbacks for EVERYTHING? gotta be elfin kidding me... Monday, April 15, 13
  • 18. no, we're not jerks. •callback: lowest Node abstraction for doing one async thing •EventEmitter: for things that happen continuously over time Monday, April 15, 13
  • 19. cb vs ee •callback: "Show me all the blueprints" •EventEmitter: "Any time you get blueprints, show me them" way of the future Monday, April 15, 13
  • 20. EventEmitter var e = new EventEmitter(); e.on('foo', function(arg) { console.log('baz' + arg) }); e.emit('foo', 'bar'); // prints 'bazbar' Monday, April 15, 13
  • 21. hello, EventEmitter var s = new http.Server() s.on('request', function(req, res){ res.end('hello, txjs!') }) s.listen(80) Monday, April 15, 13
  • 22. Streams •When managing data flows, control throughput or ENOMEM •Streams are a special EventEmitter that takes care of this •inputGoober.pipe(outputThingie) •When you grok this, you grok Node. •(see also: Marco Rogers talk) Monday, April 15, 13
  • 26. Node is JavaScript •Node is not a language - JavaScript is the language •Portability, Events, Run to completion:The JavaScript way •Not developing a language removes a huge burden. Let TC-39 andV8 fight those battles for us! •Node.js endeavors to embody the spirit of JavaScript, not to change it. Monday, April 15, 13
  • 28. Do not ignore errors •Callbacks all get an error object as their first argument •EventEmitter 'error' event is magical... MAGICALLY DEADLY! •Domains and process.on('uncaughtException') to clean up, but continuing in error is almost always terrible! •Exactly how to "not ignore" errors is a question of some debate. Liberal (never crash) vs Conservative (crash asap) Monday, April 15, 13
  • 32. Increase Portability •libuv: Evented nonblocking I/O for Computers •If you write JavaScript, it'll run on every OS Node runs on •Safety and portability are default. •Sometimes portability is compromised for "When in Rome" eg path strings: on Windows, / on Unix •If you write C++ addons, of course, all bets are off Monday, April 15, 13
  • 34. Slowness is a Bug •Latency is far and away the most important metric (Some would say, the only important metric!) •Unnecessary slowness is a bug, a sign of incorrect behavior •Necessary slowness must be isolated, reduced in scope •A slow thing should not ever make other things slow •This is what that nonblocking callback stuff is all about node's http client breaks this rule badly right now. will be fixed in 0.12! Monday, April 15, 13
  • 36. Unix Philosophy •Interfaces should be obvious, simple, composible, consistent •Callback => EventEmitter => Stream •Portability, backwards compatibility •"Every program is a proxy" - Ryan Dahl •"Don't build big apps" - Substack Monday, April 15, 13
  • 38. Assemble rather than invent •JavaScript is old •Nonblocking I/O is ancient •Unix is paleolithic •Put proven things together in simple ways •Unix philosophy applied to ideas Monday, April 15, 13
  • 40. Small Core, Small Parts •The scope of node's core API is much smaller than comparable server runtimes •No glob, no xml, etc. This is on purpose. •Small core => active userland •"The Standard Library is where modules go to die" - Python •Most npm modules are tiny, single-purpose, composible Monday, April 15, 13
  • 42. Node is a Community •Everyone is an npm publisher. Everyone is a collaborator. •Minimum Necessary Process: Governance mostly laissez-faire •Small core team, BDFL model; Expansive prolific community •Alternatives are cherished! No "one true" anything •Work together to explore the solution space. Monday, April 15, 13
  • 47. JIFASNIF •JavaScript is fun, and so Node is fun. •When making a decision, always choose the funner option •"Fun" is a very non-trivial concept! Not "easy" or "simple" or "complicated" or "featureful" or "hard" •A feeling of efficacy, rising to challenges, moving fast •It's fun to create, share, put parts together, help each other •Reduce obstacles to having fun! Monday, April 15, 13
  • 49. v0.12 •Massive cleanup of HTTP interfaces (especially client) •General purpose socket pooling interface •Faster TLS (especially initial handshake) •Better Buffer memory management Monday, April 15, 13
  • 50. v1.0.0 •The next stable release after 0.12 (probably) •Incremental improvements, uv/npm/V8 upgrades •Not much in the way of new features •Node-core's boundary is pretty much set •The Node community's future is boundless Monday, April 15, 13
  • 51. See you on the Internet! http://j.mp/node-patterns-pdf Monday, April 15, 13