The document discusses using Node.js for real-time web applications by allowing persistent connections and high concurrency. It introduces Connect, a Node.js framework that allows building reusable middleware blocks to handle tasks like logging, caching, compression etc. Examples of common middleware modules like method-override and response-time are provided. The challenges of writing scalable non-blocking servers and handling thousands of concurrent connections are addressed.
1. REAL TIME WEB
WITH NODE
By Tim Caswell
Saturday, June 5, 2010
2. Live Interaction
The web is about doing things, not just tourism anymore.
Saturday, June 5, 2010
3. Uses of Live Interaction
Chat Widget
Twitter Feed
Stock Ticker
Real-Time Game
Collaborative
Documents
TXJS Demos
Saturday, June 5, 2010
4. Why we need non-blocking
Polling is too slow and inefficient.
Live interaction requires the server to push data.
In order to push data, the connections need to be
persistent.
The server needs to handle thousands of persistent
connections
Threads aren’t going to scale.
Saturday, June 5, 2010
5. This is your server.
(with blocking I/O)
Saturday, June 5, 2010
6. This is your server
with low concurrency.
Saturday, June 5, 2010
7. This is your server
with high concurrency.
Saturday, June 5, 2010
8. Connect
We’ll use a new node
framework that
“connects” the web users
to each other.
Saturday, June 5, 2010
12. method-override.js
var key;
// Initialize any state (on server startup)
exports.setup = function (env) {
key = this.key || "_method";
};
// Modify the request stream (on request)
exports.handle = function(err, req, res, next){
if (key in req.body) {
req.method = req.body[key].toUpperCase();
}
next();
};
Saturday, June 5, 2010
13. response-time.js
exports.handle = function(err, req, res, next){
var start = new Date,
writeHead = res.writeHead;
res.writeHead = function(code, headers){
res.writeHead = writeHead;
headers['X-Response-Time'] =
(new Date - start) + "ms";
res.writeHead(code, headers);
};
next();
};
Saturday, June 5, 2010