Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Good Times
with Node.js
Matt Kemmerer
Diana Shkolnikov
MeetMe, Inc.
Topics


Motivation



Introduction to Javascript



node.js Event Loop



npm



Event Emitters



Operation order ...
Motivation

Millions

CPU Cycles
300
250

200
150
100

50
0
L1

route
request

L2

RAM

Disk

process
results

Network

fo...
Solution
•

callbacks and events

•

non-blocking I/O
single thread

•

Google V8 Engine

•
what happens under the

•
•

w...
let’s see some code…
git checkout step1
The Magic

event queue

event loop

thread pool
filesystem
network
process
other
Asynchronous Example

var http = require('http');
function onRequest(request, response) {
response.writeHead(200);
respons...
back to code…
git checkout step2
npm
npm
• https://www.npmjs.org/ : stands for node.js package manager
• FREE and open to ANYONE

• catalogs and serves all mod...
package.json
• all node.js projects should have package.json
•

npm init

: will create a package.json file

• required fi...
let’s see one…
run from project root:
npm init
Event Emitters
Event Emitters

var redis = require("redis").createClient(null, null, {});
redis.on("error", function (err) {
console.log(...
Event Emitters

•

deriving from EventEmitter class allows the subclass to emit events
and register listeners for emitted ...
let’s whip one up…
git checkout eventEmitter
EventEmitter error handling
•

if an ‘error’ event is emitted and there is no registered listener for it, the
event parame...
Operation order &
Code Flow
process.nextTick
• defers execution of argument function by placing it at the end of the event
queue
• runs before any oth...
code time again…
git checkout nextTick
look at nextTick.js
Starving I/O

code time again…
git checkout nextTick
look at compute.js
Callback Hell
Callback Hell
function myAsyncFunction(callback) {
asyncFunction1(function (err) {
if (err) {
callback(err);
return;
}
asy...
Memory Management
Memory management
• node.js has automatic garbage collection
• by default, V8 will perform garbage collections
• on failed...
Upcoming SlideShare
Loading in …5
×

Bucks County Tech Meetup: node.js introduction

1,183 views

Published on

Introduction to node.js

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Bucks County Tech Meetup: node.js introduction

  1. 1. Good Times with Node.js Matt Kemmerer Diana Shkolnikov MeetMe, Inc.
  2. 2. Topics  Motivation  Introduction to Javascript  node.js Event Loop  npm  Event Emitters  Operation order & Code Flow  async module Code can be found here: https://github.com/dshkolnikov/nodejs-intro
  3. 3. Motivation Millions CPU Cycles 300 250 200 150 100 50 0 L1 route request L2 RAM Disk process results Network format response I/O your code query db or web service write to log file
  4. 4. Solution • callbacks and events • non-blocking I/O single thread • Google V8 Engine • what happens under the • • what the developer sees… Javascript libuv • Javascript core libraries: hood… assert, http, etc. • node binding in C++
  5. 5. let’s see some code… git checkout step1
  6. 6. The Magic event queue event loop thread pool filesystem network process other
  7. 7. Asynchronous Example var http = require('http'); function onRequest(request, response) { response.writeHead(200); response.end('Hey World'); } http.createServer(onRequest).listen(8888);
  8. 8. back to code… git checkout step2
  9. 9. npm
  10. 10. npm • https://www.npmjs.org/ : stands for node.js package manager • FREE and open to ANYONE • catalogs and serves all modules ever published • Total Packages: 61,008 (as of today) • Notable packages o o o o o o o o • async express / restify mocha assert should socket.io commander Winston npm provides a CLI tool for defining, installing, and maintaining your module’s dependencies
  11. 11. package.json • all node.js projects should have package.json • npm init : will create a package.json file • required fields • name and version : do NOT put "js" or "node" in the name • general fields • main : module ID relative to the root of the package • scripts : supports a set of commands, for example start, • • npm start npm run my-script • dependencies • devDependencies • bundledDependencies • optionalDependencies : used during npm install stop, restart, test
  12. 12. let’s see one… run from project root: npm init
  13. 13. Event Emitters
  14. 14. Event Emitters var redis = require("redis").createClient(null, null, {}); redis.on("error", function (err) { console.log("Redis says: " + err); }); redis.on("ready", function () { console.log("Redis ready."); }); redis.on("reconnecting", function (arg) { console.log("Redis reconnecting: " + JSON.stringify(arg)); }); redis.on("connect", function () { console.log("Redis connected."); });
  15. 15. Event Emitters • deriving from EventEmitter class allows the subclass to emit events and register listeners for emitted events EventEmitter.emit( event, [arg1], [arg2], [...] ) EventEmitter.on( event, handlerCallback ) EventEmitter.once( event, handlerCallback ) EventEmitter.removeListener( event, handlerCallback ) EventEmitter.removeAllListeners( event )
  16. 16. let’s whip one up… git checkout eventEmitter
  17. 17. EventEmitter error handling • if an ‘error’ event is emitted and there is no registered listener for it, the event parameter will be thrown as an exception var EventEmitter = require('events').EventEmitter; var emitter = new EventEmitter(); emitter.on('error', function(err) { console.log('error: ', err); }); error: the sky is falling! emitter.emit('error', new Error('the sky is falling!')); var EventEmitter = require('events').EventEmitter; var emitter = new EventEmitter(); emitter.emit('error', new Error('the sky is falling!')); throw er; // Unhandled 'error' event ^ Error: the sky is falling! ...stack trace...
  18. 18. Operation order & Code Flow
  19. 19. process.nextTick • defers execution of argument function by placing it at the end of the event queue • runs before any other I/O events fire function foo() { console.error('foo'); } process.nextTick(foo); console.error('bar'); bar foo
  20. 20. code time again… git checkout nextTick look at nextTick.js
  21. 21. Starving I/O code time again… git checkout nextTick look at compute.js
  22. 22. Callback Hell
  23. 23. Callback Hell function myAsyncFunction(callback) { asyncFunction1(function (err) { if (err) { callback(err); return; } asyncFunction2(function (err, data) { callback(null, data); }); }); function myAsyncFunction(callback) { async.series( [ function (callback) { asyncFunction1(callback); }, function (callback) { asyncFunction2(callback); } ], function (err, data) { if (err) { callback(err); } else { callback(null, data[1]); } }); } }
  24. 24. Memory Management
  25. 25. Memory management • node.js has automatic garbage collection • by default, V8 will perform garbage collections • on failed allocations • after every N allocations • on a notification of idle condition by Node.js • pointers for memory-friendly code • global variables are never garbage collected for the life of the application, so use sparingly • be conscious of variable scope • unbind event listeners where they are no longer required

×