2. 2
Jason Gerard
•Director of Product Development, PaySpan Inc. (www.payspan.com)
•Developing software for 15 years (VB, COM, JAVA, .NET)
•Focus on SaaS
•Married with 3 kids
3. 3
What is node.js
•Created by Ryan Dahl in 2009
•Platform for asynchronous JavaScript
•Event based programming
• Event loop fundamental structure
•DIRTy
• Data Intensive Real Time
•Solution for c10k problem
4. 4
Architecture
• Written in C++ and JavaScript
• 20k lines of C++, 2k lines of JavaScript
• Built with Chrome’s V8 JavaScript Engine
• Libev
• Native Event Loop
• Libeio
• uses POSIX threads to provide async IO
• Evcom
• Stream socket library built on libev
• udns
5. 5
Events vs. Threads
• Threads
• Each request gets its own thread
• Thread can’t process another request until it’s done with current
• 2 MB stack per thread
• Context Switching
• Events
• Each request is added to a queue
• Processed by single thread
• Only one thread, no context switching
5
6. 6
Why JavaScript?
•Ubiquitous
• Everyone knows it
•“WORA”
• Stole Java’s name and beat it at it’s own game
•Front end developers can easily transition to server development
• Already used to event based programming
17. 17
Connect
• Extends HTTP support
• Adds concept of “middleware”
• function (req, res, next){
//do something
next();}
• Configured with use() method
• Order IS important
17
21. 21
Express Routing
• app.VERB() where verb is an HTTP verb (get, post, etc...)
• app.all() execute for all verbs
• req.query for querystring parameters
• req.body for POST, must use bodyParser middleware
21
30. 30
Consolidate
• Provides support for many view engines
• Exposes each view engine as Express middleware
• Wraps each view invocation in function
• function x(path[, locals], callback)
• Same format Express uses
30
33. 33
Socket IO
• Realtime web apps
• Broad browser support
• Internet Explorer 5.5+
• Safari 3+
• Google Chrome 4+
• Firefox 3+
• Opera 10.61+
• iPhone Safari
• iPad Safari
• Android WebKit
• WebOs WebKit 33
• Falls back to next best transport
• WebSocket
• Adobe® Flash® Socket
• AJAX long polling
• AJAX multipart streaming
• Forever Iframe
• JSONP Polling
34. 34
Socket.IO vs SignalR
• Socket.IO
• Broader browser support
• More fallback transports
• JavaScript for client and
Server
34
• SignalR
• Less Browser support
• Less transports
• .NET code on server
35. 35
Data Access
• Drivers exists for most databases
• Mongo
• CouchDB
• SQL Server
• MySQL
• PostgreSQL
• unixODBC
35
36. 36
SQL Server
• node-sqlserver
• Microsoft Provided module
• https://github.com/WindowsAzure/node-sqlserver
• edge.js
• execute SQL using the .NET SQL Client
• supports CRUD operations
36
37. 37
SQL DRIVER
• node-sqlserver
• Developed by Microsoft
• Not a lot of activity on
GitHub. (38 open
issues)
• Not production ready
37
41. 41
Node Process Direct
• Simplest option
• Doesn’t reload changed files
• requires restart
• Not a service out of the box on windows
• Easily fixed (winser npm module)
• Not good at serving static content
• Good for development, though nodemon is better choice
41
42. 42
nginx
• Extremely fast HTTP server
• Great for static content
• Proxy Dynamic Calls to Node
• Much faster than Apache
• Poor windows support
• current version considered beta
• “high performance and scalability should not be expected”
42
43. 43
iisnode
• Created by Tomas Janczuk
• Native IIS module
• Powers Node on Windows Azure
• Windows only (if that’s not obvious you’re at the wrong conference)
43
49. 49
SDK Features
• Tables
• create and delete tables
• create, query, insert, update, merge, and delete entities
• Blobs
• create, list, and delete containers, work with container metadata and permissions, list blobs in
container
• create block and page blobs (from a stream, a file, or a string), work with blob blocks and pages,
delete blobs
• work with blob properties, metadata, leases, snapshot a blob
• HD Insight
• create, list and delete HDInsight clusters
49
50. 50
SDK Features Continued
• Storage Queues
• create, list, and delete queues, and work with queue metadata
• create, get, peek, update, delete messages
• Service Bus
• Queues: create, list and delete queues; create, list, and delete subscriptions; send, receive, unlock and delete messages
• Topics: create, list, and delete topics; create, list, and delete rules
• Notification hubs: create hubs, register for messages, send messages
• Azure SQL Database
• create, list and delete Azure SQL Database servers, databases and firewall rules
• Service Runtime
• discover addresses and ports for the endpoints of other role instances in your service
• get configuration settings and access local resources
• get role instance information for current role and other role instances
• query and set the status of the current role
50
51. 51
Deploying to azure
npm install azure
git commit -m "My first Node app"
git push azure master
•IT’S THAT EASY!
51
61. 61
FInal Thoughts
• Node is for IO intensive applications
• Web Applications
• 1000s of concurrent connections
• NOT for Compute bound operations
• Don’t try to calculate PI and expect a responsive server
• Fast development, easy deployment
• No more edit, build, wait, refresh -- just edit and refresh!
61
c10k - Ten Thousand Clients - solves problem through async IO
Libeio uses a threadpool to provide an async version of the POSIX IO libraries
Starbucks analogy Events: Order and then step aside until order is called Walmart - Threads: Order and wait until food is ready, no one else can order on that register until done Event LOOP - talk about it
Only the IO is non blocking. Compute bound operations (like calculating PI) will still block. Web apps shouldn’t be compute bound though.
Show example with setTimeout and c# (sleep.js and sleep.cs)
events1.js -- use telnet, not nc
addListener and on are synonymous once - removes listener the first time the event is received all methods return the emit so chaining is possible
events2.js MyEventClass.js
console example
http.js example
Connect has a router now
Supports filters
if empty was false, the last <p> tag would be gone
db.getUsers() - Remember, everything is a callback
Example... socketio express app
Unless otherwise noted below, all measurements were taken at server CPU utilization > 90%. The configuration involved one quad core Windows 2008 server with node.js v0.6.6 and iisnode v0.1.13. Depending on scenario, one or two WCAT client machines were required to saturate the server. Clients and server were connected to the same switch but were not on private network. [1] See test code . In this scenario node.js application returns a simple “hello, world” plaintext response to an HTTP GET request. This is the only scenario that uses a single node.exe instance (both self-hosted and IIS hosted). Given the single-threaded nature of node.exe, the multi-core server CPU is never fully utilized. However, the node.exe process has consistently utilized > 23% of CPU, which corresponds to almost full utilization of a single core of the quad-core server. [1.1] Although in this scenario iisnode achieved better throughput than self-hosted node.exe, it was accomplished with almost twice as large CPU utilization (~60% in case of iisnode compared to ~30% in case of node.exe). [2] See test code . This scenario uses the same node.js “hello world” application as scenario #1, but it utilizes 4 node.exe processes to saturate the CPU on the quad core server. In case of self-hosting node.exe, the built-in cluster functionality is employed. In case of iisnode, the built-in capability to spawn and route to multiple node.exe processes per application is used. [3] See test code . This scenario simulates using node.js to serve a web site using express - one of the most popular MVC frameworks for node. The web site consists of an HTTP endpoint that dynamically generates HTML that displays a randomly generated order information using the jade rendering engine. The HTML refers to a static CSS file. As part of each WCAT transaction, one request is made to the HTML endpoint that generates dynamic content and the another to get the static CSS file. In case of self-hosted node.js application, the static file is served by the express framework using the connect.static middleware (which is built into express and a most likely mechanism to be used by someone serving static content from express). In case of IIS, requests for static content are actually not handled by iisnode at all – a URL rewrite module causes that request to be handled by the native static file handler (which plays to the “side by side with other content types” benefits of iisnode). [4] See test code . This scenario simulates using node.js to implement web APIs: HTTP endpoints that generate dynamic content. In this particular case we are reusing the order generation and HTML rendering endpoint from scenario #3, without including a request for static CSS file in the WCAT transaction.