© Copyright 2012 Avanade Inc. All Rights Reserved.
The Avanade name and logo are registered trademarks in the US and other countries.
var about = {
Topic: ‘Node.js’,
Presenter : 'Jason Park‘
}
Brown Bag Session
Quick tour on Node JS
© Copyright 2012 Avanade Inc. All Rights Reserved.
Who’s talking
• Singapore Avanade App Dev family
• ASP.NET (MVC) / C# guy
• Tech lover
2
© Copyright 2012 Avanade Inc. All Rights Reserved.
What is Node JS?
• Node.js uses an event-driven, non-blocking I/O
model that makes it lightweight and efficient, perfect
for data-intensive real-time applications that run
across distributed devices
NodeJS.org
3
© Copyright 2012 Avanade Inc. All Rights Reserved.
What is Node JS?
4
• 2nd most popular project in GitHub
© Copyright 2012 Avanade Inc. All Rights Reserved.
What is Node JS?
• Server-side Javascript
• Event-driven
• Built on Chrome V8 engine
• NPM (like NuGet for .NET)
5
© Copyright 2012 Avanade Inc. All Rights Reserved.
What is Node JS?
6
© Copyright 2012 Avanade Inc. All Rights Reserved.
Server-Side JS?
• But wait, server-side JS[1] has been around since 1996!
• What's makes Node JS so special?
[1] Netscape Enterprise Server
7
© Copyright 2012 Avanade Inc. All Rights Reserved.
Speed
• Handles thousands of requests/sec with no performance
degradation
• Ideal for hosting a single-page application - highly
concurrent server application
8
© Copyright 2012 Avanade Inc. All Rights Reserved.
Speed
9
© Copyright 2012 Avanade Inc. All Rights Reserved.
How?
• Keep slow operations from blocking other operations
10
© Copyright 2012 Avanade Inc. All Rights Reserved.
How?
11
© Copyright 2012 Avanade Inc. All Rights Reserved.
How?
• 1 Threaded Application
PROS: Good for conceptual simplicity
CONS: Bad for CPU-bound algorithm
12
© Copyright 2012 Avanade Inc. All Rights Reserved.
How?
• Traditional I/O
13
© Copyright 2012 Avanade Inc. All Rights Reserved.
How?
• JavaScript is already geared towards event-based
programming
14
© Copyright 2012 Avanade Inc. All Rights Reserved.
How?
• Async I/O
No need to wait for disk.
Do something else meanwhile!
15
© Copyright 2012 Avanade Inc. All Rights Reserved.
HTTP Server
16
© Copyright 2012 Avanade Inc. All Rights Reserved.
TCP Server
17
© Copyright 2012 Avanade Inc. All Rights Reserved. 18
© Copyright 2012 Avanade Inc. All Rights Reserved.
Consider Node JS if you
• Want to have a website handling large number of
concurrent connections
• Want to quickly build an Ajax-rich web application
• Want to learn JavaScript/Event-based programming
19
© Copyright 2012 Avanade Inc. All Rights Reserved.
Consider other options if you
• Want something mature
• Want a framework with enterprise-level security proven in
the market
• Need IDE like Visual Studio for coding
20
© Copyright 2012 Avanade Inc. All Rights Reserved.
Big Names using Node JS
• Microsoft
– Azure & WebMatrix support
• Linked In
– Mobile application running on Node JS
• Wallmart
– Mobile application running on Node JS
21
© Copyright 2012 Avanade Inc. All Rights Reserved.
Questions?
22

Brownbag on basics of node.js

  • 1.
    © Copyright 2012Avanade Inc. All Rights Reserved. The Avanade name and logo are registered trademarks in the US and other countries. var about = { Topic: ‘Node.js’, Presenter : 'Jason Park‘ } Brown Bag Session Quick tour on Node JS
  • 2.
    © Copyright 2012Avanade Inc. All Rights Reserved. Who’s talking • Singapore Avanade App Dev family • ASP.NET (MVC) / C# guy • Tech lover 2
  • 3.
    © Copyright 2012Avanade Inc. All Rights Reserved. What is Node JS? • Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices NodeJS.org 3
  • 4.
    © Copyright 2012Avanade Inc. All Rights Reserved. What is Node JS? 4 • 2nd most popular project in GitHub
  • 5.
    © Copyright 2012Avanade Inc. All Rights Reserved. What is Node JS? • Server-side Javascript • Event-driven • Built on Chrome V8 engine • NPM (like NuGet for .NET) 5
  • 6.
    © Copyright 2012Avanade Inc. All Rights Reserved. What is Node JS? 6
  • 7.
    © Copyright 2012Avanade Inc. All Rights Reserved. Server-Side JS? • But wait, server-side JS[1] has been around since 1996! • What's makes Node JS so special? [1] Netscape Enterprise Server 7
  • 8.
    © Copyright 2012Avanade Inc. All Rights Reserved. Speed • Handles thousands of requests/sec with no performance degradation • Ideal for hosting a single-page application - highly concurrent server application 8
  • 9.
    © Copyright 2012Avanade Inc. All Rights Reserved. Speed 9
  • 10.
    © Copyright 2012Avanade Inc. All Rights Reserved. How? • Keep slow operations from blocking other operations 10
  • 11.
    © Copyright 2012Avanade Inc. All Rights Reserved. How? 11
  • 12.
    © Copyright 2012Avanade Inc. All Rights Reserved. How? • 1 Threaded Application PROS: Good for conceptual simplicity CONS: Bad for CPU-bound algorithm 12
  • 13.
    © Copyright 2012Avanade Inc. All Rights Reserved. How? • Traditional I/O 13
  • 14.
    © Copyright 2012Avanade Inc. All Rights Reserved. How? • JavaScript is already geared towards event-based programming 14
  • 15.
    © Copyright 2012Avanade Inc. All Rights Reserved. How? • Async I/O No need to wait for disk. Do something else meanwhile! 15
  • 16.
    © Copyright 2012Avanade Inc. All Rights Reserved. HTTP Server 16
  • 17.
    © Copyright 2012Avanade Inc. All Rights Reserved. TCP Server 17
  • 18.
    © Copyright 2012Avanade Inc. All Rights Reserved. 18
  • 19.
    © Copyright 2012Avanade Inc. All Rights Reserved. Consider Node JS if you • Want to have a website handling large number of concurrent connections • Want to quickly build an Ajax-rich web application • Want to learn JavaScript/Event-based programming 19
  • 20.
    © Copyright 2012Avanade Inc. All Rights Reserved. Consider other options if you • Want something mature • Want a framework with enterprise-level security proven in the market • Need IDE like Visual Studio for coding 20
  • 21.
    © Copyright 2012Avanade Inc. All Rights Reserved. Big Names using Node JS • Microsoft – Azure & WebMatrix support • Linked In – Mobile application running on Node JS • Wallmart – Mobile application running on Node JS 21
  • 22.
    © Copyright 2012Avanade Inc. All Rights Reserved. Questions? 22

Editor's Notes

  • #2 Hello and thank you for joining me for today’s brown bag session on Node JS. Throughout the presentation, I’ll share what Node JS is about and wrap it up with a short demo to create a chat app using it. My name is Jason Park
  • #3 And as some of you guys may know, I’m a member of Singapore App Dev family. And like most of you guys, I’m a C# guy with some ASP.NET background. I’m just a tech lover and who loves playing around with new stuffs related to web, but not an expert in Node JS. I’ll try my best to answer your questions but please understand if I have some trouble answering your question.
  • #4 Ok, let’s go straight into the point. What is Node JS? Node JS website describes it as a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices Keywords in this statement are ‘event driven’, ‘non blocking’ and ‘real time application’. We’ll cap these again later in the slides. Now you might ask yourself, it is one of hundred choices we've got out there.  why should you care about it?   yes it's one of many platforms available out there in the market but at the same time it's one of the hottest web platforms as well. I wonder how many of you have heard about GitHub.  It's is a web-based hosting service for software development projects that use the Git revision control system, and is where majority of open source collaborations are happening nowadays.
  • #5 Node is currently ranked 2nd, higher than jQuery, which’s being used all over the web nowadays. That’s just remarkable, isn’t it?
  • #6 traditionally, javascript has been running on the web browsers (and in fact in most cases, it still is).  we call it client-side javascript.  server side javascript is as its name implies, javascript running on the server.  its significance is that the server and client speaks the same language, so data that's passed between the two do not need a conversion.  also uniqueness of javascript is enables some of the key behaviors of Node JS such as an event driven programming. http://stackoverflow.com/questions/5599024/what-so-different-about-node-jss-event-driven-cant-we-do-that-in-asp-nets-ht V8 engine is a Google's open source javascript engine written in C++ and as you all know, it's blazing fast. NodeJS makes use of the V8 engine to process javascript commands and internal process management. NPM stands for Node Package Management. It has a very active open source community just like NuGet of our .NET family.  In fact, NPM It works pretty similar to NuGet for .NET, and it’s just as easy as using NuGet on visual studio. Almost a no-brainer.  I’ll show you an example on NPM is during the demo.
  • #7 This is what Node JS consists of. NodeJS is built on V8 and number of open source libraries such as Crypto and DNS libraries. Then exposes system APIs through node bindings and standard libraries. I won’t go into details for this section.
  • #8 Back to software aspect of the Node JS, Some of you may heard about server side Javascript from somewhere else. And you’re probably correct. It’s been around since 1996. There was a Netscape Enterprise Server which runs Javascript on server as well. But it wasn’t really a big hit. Then what makes Node JS so special and draws attention after 1.5 decade?
  • #9 Simply put.  Speed.   Javascript has evolved a lot since 1996 and introduction of speedy V8 engine allows full usage of javascript as a server side language. It easily handles thousands of requests/sec with no performance degradation.  making it ideal for hosting a single page application - which keeps connection live between the server and the browser all the time. Think of Facebook chat or even gmail.  In traditional technology stacks, usually each connection is managed by a dedicated thread, so 1000 live connections would translate to 1000 threads in the server.  
  • #10 This shows how conventional technology stack struggles once it hits 1k concurrent connections. If you were to build some sort of a chat app or gmail-like website which keeps connection alive between the server and the browser, there’s a good reason to consider Node JS.
  • #12 This is Node JS processing model.  When the client sends HTTP request to Node JS server, single threaded Node JS application passes the request details as a Javascript closure to one of its worker with callbacks. I won’t go into details on what closure is about, just think of it as a function being passed as an object for now. I’ll continue. The workers are managed by its internal C++ threadpool which runs asynchronously.  One of the core ideas behind Node JS is that thread management is difficult and can be easily abused unless you're an expert, or even if you're an expert.  So in Node JS programmers are expected to focus on the programming, and let the framework handle the thread management. Anyway once the non-blocking worker is done with proccessing supposedly expensive tasks, it sends result back to the main thread via callback and the event loop returns it to the client. was it clear?  please feel free to raise your hands if you've got questions.
  • #13 Please allow me to recap a couple of reasons behind the 1 threaded application. First, You have to be a very talented programmer to design programs that work perfectly in a threaded environment. Threads are just hard. You have to be a good and very good to maintain a threaded project. There are just so many problems that can be hard to avoid in very large projects. Secondly the whole platform was designed to be run asynchronously. A node.js request idle's when it's not being used or when it's waiting for IO. So NodeJS can easily handle 10k clients at the same time in parallel on one development machine. As for threads and cores. Node.js is single threaded and scales by creating multiple node processes. It does the load balancing and thread management for you at the framework level. But there's a downside as well and this could be critical.  As it's a single threaded, any unhandled deadlock or code that slows the program will affect the entire application.  Whereas in a scenario where an individual thread is dedicated per connection, it’ll affect that specific thread only.
  • #14 In a traditional programming, to read data from a file, you'd write a code like this.  this is synchronous programming. Everything flows in sequence so there’s no logic running in parallel. When something gets stuck in the upstream you just gotta wait.
  • #15 However since Javascript is already geared towards event-based, it can easily support asynchronous programming using callback functions. In fact it’s one of the key features of the Javascript as user behavior isn’t always straightforward or predictable. The code in the slide represents binding an event handler on the page.
  • #16 You should be familiar with asynchronous programming if you've played around with .NET 4.5 its core idea is to allow application logic to flow without blocking. In the example, DoSomethingElse() does not need to wait for file to be read. The file read will be handled by a subworker and dosomethingwithdata function will be handled by another worker once the file read is complete. We call it event-based. I used file read as an example but same approach applies to database read/write, web service and all the time consuming
  • #17 Now, it’s time for some Node JS code. This is how you create a webserver in Node JS. Just with 5 lines of code, I can host a webpage. (demo) I bounded an anonymous function to be called for handling HTTP requests. So for every request, it’ll invoke the function and will always return a page with ‘Hello World’ text message.
  • #18 Similarly you can host a TCP server as well. Instead of creating a HTTP server, I created a server using NET library. And the rest work in pretty similar fashion. Every request gets handled by the anonymous function to echo messages.
  • #19 I bet I had enough of talking, so I’ll go through a quick demo on implementing a single page chat app using Node JS.
  • #20 You may consider using Node JS for your side project if you want to build a website that handles large number of concurrent connection.  Or to build an Ajax-rich web application.   Or if you want to learn Javascript or event based programming, Node JS is a pretty decent place to start as well.  During the process, you'll get to feel the power of open source as well.  They have an amazing community out there in public. -- Feel the power of open source fast/unencumbered development process
  • #21  But if you have plans to build a secure and reliable website or even service, you'd better consider other mature options like ASP.NET. It's too young and changes too rapidly. Also if you're a kind of a guy who must use IDE to code, oh then you should better stick to ASP.NET.  Visual Studio is probably by far the most handy IDE that you'll ever come across.
  • #23 Questions ---- Thank you for joining today’s session. Hope you enjoyed Node JS.