17. Asynchronous
Everything
Saturday, April 17, 2010
18. bit.ly/nodejs-fstream
function upload_file(req, res) {
req.setBodyEncoding('binary');
var stream = new multipart.Stream(req);
stream.addListener('part', function(part) {
part.addListener('body', function(chunk) {
var progress = (stream.bytesReceived / stream.bytesTotal *
100).toFixed(2);
var mb = (stream.bytesTotal / 1024 / 1024).toFixed(1);
sys.print("Uploading "+mb+"mb ("+progress+"%)015");
// chunk could be appended to a file if the uploaded file needs to
be saved
});
});
stream.addListener('complete', function() {
res.sendHeader(200, {'Content-Type': 'text/plain'});
res.sendBody('Thanks for playing!');
res.finish();
sys.puts("n=> Done");
});
}
Saturday, April 17, 2010
19. Javascript is
great for this.
Saturday, April 17, 2010
24. Real-Time
Applications
Saturday, April 17, 2010
25. WebSockets
• Persistent server connections
• Part of HTML5
• True real-time for web apps
• Deprecate to Flash Sockets
Saturday, April 17, 2010
26. Why Real-Time?
• Better on your server
• Better for your user
• You need more buzzwords
Saturday, April 17, 2010
27. var socket = new WebSocket("ws://www.websocket.org");
socket.onopen = function(evt) { alert("Open."); };
socket.onmessage = function(evt) { alert(evt.data); };
socket.onclose = function(evt) { alert("Closed."); };
socket.send("Hello Web Socket!");
socket.close();
Saturday, April 17, 2010
28. Why JS for WebSockets?
• You already write your client
interaction code in Javascript
• Just an extension of that
• Same interface throughout
Saturday, April 17, 2010
29. Hazards of a
Young Tool
Saturday, April 17, 2010
30. node.websocket.js
• Framework-like approach
• Includes fun examples
• Node version headaches
• github.com/guille/node.websocket.js/
Saturday, April 17, 2010
31. Socket.IO
• Multi-transport socket for Node
• Comes with client JS library
• For production usage
• github.com/rosepad/socket.io-node
Saturday, April 17, 2010
32. node.ws.js
• Minimal Node WebSocket server
• Talk to your browser clients
• Javascript all the way down
• Mostly for experimentation
• github.com/ncr/node.ws.js
Saturday, April 17, 2010
33. How can we
actually use it?
Saturday, April 17, 2010
34. Redis is the Bridge
• Super-fast, super-awesome
• PUBSUB = WIN!
• Swiss-army knife for your app.
Saturday, April 17, 2010
35. Example
• Twitter-esque status streams
• Want to update web interface in
real time
• Rails, Node, Redis, and Chrome
Saturday, April 17, 2010
37. class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable and :timeoutable
devise :database_authenticatable, :registerable, :rememberable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation
def follow(other_user)
Red.sadd "user:#{other_user.id}:followers", self.id
Red.sadd "user:#{self.id}:follows", other_user.id
end
def follower_ids
Red.smembers "user:#{self.id}:followers"
end
def follow_ids
Red.smembers "user:#{self.id}:follows"
end
def update(text)
(follower_ids + [self.id]).each do |uid|
Red.lpush "user:#{uid}:timeline", text
Red.lpush "user:#{uid}:updates", text
Red.publish "user:#{uid}:timeline", text
end
end
end
Saturday, April 17, 2010
38. def update(text)
(follower_ids + [self.id]).each do |uid|
Red.lpush "user:#{uid}:timeline", text
Red.lpush "user:#{uid}:updates", text
Red.publish "user:#{uid}:timeline", text
end
end
Saturday, April 17, 2010
54. Tip of the Iceberg
• Node’s libraries and use cases
are expanding rapidly
• Async means thinking differently
• Still at the early stages
Saturday, April 17, 2010