Node.js for .net developers

Post on 01-Sep-2014

7.484 views 0 download

Tags:

description

 

Transcript of Node.js for .net developers

Node.jsFor an everyday .Net web developer

Cristian PrietoSenior Developer at Readify@cprieto / www.cprieto.com

www.buildwindows.com

Who am I?• Senior Software developer in Readify• Polyglot programmer• Javascript junkie• Very opionated• A “web guy”• Microsoft ASP.NET MVP• Geek?

www.buildwindows.com

What’s Node.js?• Asynchronous event-driven I/O server-

side JavaScript server supporting CommonJS and based in an event-loop engine using the Chrome’s V8 engine and libevent (and now libuv) libraries.

• Created by Ryan Dahl in 2009• ~ 2K lines of Javascript, > 20K lines of

C/C++• It’s cancer (Ted Dziuba)• No, it’s not (Node.js users)

So that’s all…

Questions?

“Piece by piece”

Jack the Ripper

I/O Event-loop based server

A tale about bunnies, hamsters and Squids…

A hamster visiting your site

Single threaded bunny

(a happy bunny)

Your web server

Impatient hamsters

Single threaded bunnyYour web server

(a busy bunny!)

Multi-threaded bunnyYour web server

Fetching database (2s)

Consuming Service (3s)

Writing to a file (3s)

Event-loop based serverHyperactive squid

Constantly check who is idle doing nothing or waiting

Well, That’s Node.js

www.buildwindows.com

Yes… JavaScript…

www.buildwindows.com

A JavaScript Server?• It’s not a new idea, there were a lot of

failed attempts out there• You are a web developer, so you are

comfortable with it, right?• Javascript is naturally asynchronous,

closure/callback function lover language

• Javascript is a hack language• jQuery? DOM hack

www.buildwindows.com

Bad code

www.buildwindows.com

Good code

www.buildwindows.com

Node.js components• Core or kernel• Very small, compact, cross platform

• Modules• User contributed, like packages• NPM is your friend (sorry, no Windows!)

• Core is single threaded• Modules spawn new process or

threads• But you should not care about that

• Core “automatically” load balance between cores/cpu’s

www.buildwindows.com

Some useful modules• Network modules• Http, Net, Dns, smtp, Socket.IO

• Web framework modules• Express, Fab.js

• Testing• Vows, jasmine-node, Cup of Tea

• Database connection modules• File servers, payment, graphics

www.buildwindows.com

But I’m a Windows developer • Microsoft loves JavaScript• Microsoft loves Node.js• Microsoft is going to deploy Node.js• Bing is hiring Node.js devs• Joyent is improving Node.js on Windows

• IIS loves Node.js• IISNode• IISNode Express

• Webmatrix loves Node.js• Node.js templates for Webmatrix

DEMOWarning… JavaScript ahead!

www.buildwindows.com

Not everything is gold• When to use Node.js• I/O heavy processes• Chatty protocols• Execution pipes

• When not to use Node.js• Computer intensive operations (but…)• Memory intensive operations (1.2G stack)• You still afraid of JavaScript

Ok, nice, I got it…

But I’m a .Net developer

Surprising Fact

• Most Unix developers don’t do Windows

www.buildwindows.com

We can do it• IO Completion Ports (IOCP), WinNT 3.5• IIS uses it since IIS5.0• IHttpAsyncHandler (since 2003!)• IO threads are not Worker threads• Eventing, declarative instead of

imperative• ASP.NET 4.5 supports• Async HttpHandlers• Async HttpModules• Async MVC Controllers

• C# 5.0 async/await

www.buildwindows.com

Threads are not magic

• So “new Thread” it’s not a magical wand

• Context switching it’s not free• Tasks != Threads

www.buildwindows.com

What should I do now?• Go and learn JavaScript• Go and “play” with Node.js• Go and read a good async book• Don’t use threads as your hammer

• Understand threading implications• Realize there is not a single powerful

language• Buy me a coffee

Thanks!

Questions?