SlideShare a Scribd company logo
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

More Related Content

What's hot

javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
Domingo 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 Ruby
sikachu
 
Building MapAttack
Building MapAttackBuilding MapAttack
Building MapAttack
Kyle Drake
 
Next generation frontend tooling
Next generation frontend toolingNext generation frontend tooling
Next generation frontend tooling
pksjce
 
Scalable Web Apps
Scalable Web AppsScalable Web Apps
Scalable Web Apps
Piotr Pelczar
 
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
Charles 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 applications
Ondrej Mihályi
 
Children of Ruby
Children of RubyChildren of Ruby
Children of Ruby
Simon St.Laurent
 
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
Ondrej Mihályi
 
Rails development environment talk
Rails development environment talkRails development environment talk
Rails development environment talk
Reuven Lerner
 
Test::Kantan - Perl and Testing
Test::Kantan - Perl and TestingTest::Kantan - Perl and Testing
Test::Kantan - Perl and Testing
Tokuhiro 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 trash
infodox
 
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 home
jaredhaight
 
BSides Hannover 2015 - Shell on Wheels
BSides Hannover 2015 - Shell on WheelsBSides Hannover 2015 - Shell on Wheels
BSides Hannover 2015 - Shell on Wheels
infodox
 
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
twopoint718
 
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
 
Packers
PackersPackers

What's hot (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 to 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 Summit
Ran 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 ops
Michael Brunton-Spall
 
Rust's Journey to Async/await
Rust's Journey to Async/awaitRust's Journey to Async/await
Rust's Journey to Async/await
C4Media
 
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
Konrad 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 hadoop
Russell 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 vote
dreamwidth
 
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
Robert Pearce
 
node.js in action
node.js in actionnode.js in action
node.js in action
Karan 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 keynote
Sana 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-attac
Priyanka Aash
 
Static-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptxStatic-Analysis-in-Industry.pptx
Static-Analysis-in-Industry.pptx
ShivashankarHR1
 
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
fisherwebdev
 
Node.js: CAMTA Presentation
Node.js: CAMTA PresentationNode.js: CAMTA Presentation
Node.js: CAMTA Presentation
Rob Tweed
 
Intro to JavaScript Testing
Intro to JavaScript TestingIntro to JavaScript Testing
Intro to JavaScript TestingRan Mizrahi
 

Similar to 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
 

Recently uploaded

A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Crescat
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
vrstrong314
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 

Recently uploaded (20)

A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
Introducing Crescat - Event Management Software for Venues, Festivals and Eve...
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
Nidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, TipsNidhi Software Price. Fact , Costs, Tips
Nidhi Software Price. Fact , Costs, Tips
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket ManagementUtilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
Utilocate provides Smarter, Better, Faster, Safer Locate Ticket Management
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 

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