The document is a presentation about Node.js, a JavaScript runtime built on Chrome's V8 JavaScript engine. It discusses how Node.js uses an event-driven, non-blocking I/O model that makes it particularly suited for real-time web applications and I/O-intensive applications compared to traditional threaded server models. It provides examples of Node.js features like asynchronous I/O, event loops, modules and the npm package manager.
6. static void Main() Normal Method Call
{
int result = Add(2, 2);
Console.WriteLine("2+2 = {0}", result);
}
static int Add(int n1, int n2)
{
return n1 + n2;
}
Does some stuff in processor cache or RAM
7. static void Main() Normal Method Call
{
string name = GetName();
Console.WriteLine("Hi {0}", name);
}
static string GetName()
{
return Console.ReadLine();
}
Blocks until ready
8. What’s the big deal?
Well, let’s think about what we do in a web server...
9. public ActionResult View(int id)
{
var product = northwindDataContext.Get(id);
return View(product);
}
Blocking Network Call (IO)
16. “we’re doing it wrong”
- Ryan Dahl, node.js creator
http://www.yuiblog.com/blog/2010/05/20/video-dahl/
17. So I can guess what you’re thinking,
“What about multi-threading?”
18. Yep, mainstream web servers like Apache and IIS* use a
Thread Per Connection
* Well, technically IIS doesn’t quite have a thread per connection as there’s some kernel level stuff which will talk to IIS/
ASP.NET depending on your configuration and it’s all quite complicated. But for the sake of this presentation we’ll say
it’s a thread per connection as it’s pretty darn close in all practical terms.
If you’d like to talk about this more feel free to chat to me afterwards, I love spending my spare time talking about
threading in IIS.
HONEST.
28. We’d have to completely change how we write code
public ActionResult View(int id)
{
var product = northwindDataContext.Get(id);
return View(product);
Blocking Call
}
29. To something where we could easily write non-blocking code
public void View(HttpResponse response, int id)
{ Non-blocking Call
northwindDataContext.Get(id, (product) => {
response.View(product);
});
}
Anonymous Function
Callback
32. Even if we had a language that made
writing callbacks easy (like C#), we’d need
a platform that had minimal to no
blocking I/O operations.
33. If only we had a language which was designed to
be inherently single threaded, had first class
functions and closures built in, and had no
preconceived notions about I/O?
Wouldn’t it also be really handy if half* of the
developers in the world already knew it?
(you can probably guess where this is going...)
*The guy speaking completely made that up for the sake of this slide. but he’s pretty sure there are quite a few
49. Every request is just a callback
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from node.js!');
});
server.listen(3000);
console.log('Server started on 3000');
50. var fs = require('fs'),
Doesn’t block to read file
http = require('http');
var server = http.createServer(function(req, res) {
fs.readFile('./data.txt', function(err, data) {
if (err) {
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end(err.message);
} else {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end(data);
}
});
}); Server can deal with other requests while waiting for file
server.listen(3000);
console.log('Server listening on 3000');
51. Node libraries are structured as
CommonJS modules
http://www.commonjs.org/specs/modules/1.0/
55. /* people.js */
function Person(name) {
this.name = name;
}
Person.prototype = {
sayHi: function() {
console.log("Hi, I'm " + this.name);
}
} Attaching API to exports
exports.createPerson = function(name) {
return new Person(name);
};
56. exports from module are returned
var people = require('./people'); File path
var barry = people.createPerson('Barry');
barry.sayHi(); // Hi, I'm Barry
console.log( typeof Person ); // undefined
Internals of module don’t exist in this context
57. Despite being a relatively new runtime,
there are a massive number of open source libraries available
58. Official list alone has 718 released modules
https://github.com/ry/node/wiki/modules
(as of Feb 2011)
59. Modules for just about everything you could think of,
•Web Application Servers
•Static File Servers
•Web Middleware
•Database (couchdb, mongodb, mysql, postgres, sqlite, etc..)
•Templating
•Build & Production
•Security
•SMTP
•TCP/IP
•Web Services
•Message Queues
•Testing
•XML
•Command Line Parsers
•Parser Generators
•Debugging tools
•Compression
•Graphics
•Payment Gateways
•Clients for many public API’s (facebook, flickr, last.fm, twitter, etc...)
•Internationalization
and probably a lot for things you’ve never heard of
60. So you can just pull them down and reference
them on their files.
However once libraries start depending on
other libraries, and we start installing quite a few,
Well, we know how this ends up...
63. npm is a package manager for
node. You can use it to
install and publish your
node programs. It manages
dependencies and does other
cool stuff.
http://npmjs.org/
75. Although it’s nice to think about writing applications
for just new versions of Chrome, Firefox and IE9.
We should strive to get our applications working
everywhere*.
*to at least some degree
84. (function(exports) {
exports.createChartOptions = function(data) {
var values = parseData(data);
return {
...
};
}
})(typeof window !== 'undefined' ? (window.demo = {}) : exports);
Attaches API to Attaches API to exports in node.js
window.demo in browser to be returned by require
89. But it’s only in very recent browsers
And due to protocol concerns it’s now disabled even in recent browsers
90. But you’ve probably noticed that
applications have been doing this for years
We’ve got some pretty sophisticated
methods of emulating it, even in IE
91.
92. WebSockets
Silverlight / Flash
IE HTML Document ActiveX
AJAX Long Polling
AJAX multipart streaming
Forever IFrame
JSONP Polling
93. All of those require a server holding open a
connection for as long as possible.
101. If you’re on Mac or Linux then you’re good to go
http://nodejs.org/
102. If you’re on Windows
you’re going to be a little
disappointed
103. Currently the best way to get start on Windows is
hosting inside a Linux VM and ssh’ing it.
http://www.lazycoder.com/weblog/2010/03/18/getting-started-with-node-js-on-windows/
Proper Windows support will be coming soon.
http://nodejs.org/v0.4_announcement.html
104. I think that node is super exciting and that you
should start experimenting with it today
105. But even if you don’t, other Web
Frameworks are watching and it will
almost certainly influence them
109. Image Attributions
Old man exmouth market, Daniel2005 - http://www.flickr.com/photos/loshak/530449376/
needle, Robert Parviainen - http://www.flickr.com/photos/rtv/256102280/
Skeptical, Gabi in Austin - http://www.flickr.com/photos/gabrielleinaustin/2454197457/
Javascript!=Woo(), Aaron Cole - http://www.flickr.com/photos/awcole72/1936225899/
Day 22 - V8 Kompressor, Fred - http://www.flickr.com/photos/freeed/5379562284/
The Finger, G Clement - http://www.flickr.com/photos/illumiquest/2749137895/
A Messy Tangle of Wires, Adam - http://www.flickr.com/photos/mr-numb/4753899767/
7-365 I am ready to pull my hair out..., Bram Cymet - http://www.flickr.com/photos/bcymet/3292063588/
Epic battle, Roger Mateo Poquet - http://www.flickr.com/photos/el_momento_i_sitio_apropiados/5166623452/
World's Tiniest, Angelina :) - http://www.flickr.com/photos/angelinawb/266143527/
A Helping Hand, Jerry Wong - http://www.flickr.com/photos/wongjunhao/4285302488/
[108/365] Ill-advised, Pascal - http://www.flickr.com/photos/pasukaru76/5268559005/
Gymnastics Artistic, Alan Chia - http://www.flickr.com/photos/seven13avenue/2758702332/
metal, Marc Brubaker - http://www.flickr.com/photos/hometownzero/136283072/
Quiet, or You'll see Father Christmas again, theirhistory - http://www.flickr.com/photos/
22326055@N06/3444756625/
Day 099, kylesteed - http://www.flickr.com/photos/kylesteeddesign/4507065826/
Spectators in the grandstand at the Royal Adelaide Show, State Library of South Australia - http://
www.flickr.com/photos/state_library_south_australia/3925493310/