Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Comet with node.js and V8

1.
Comet with node.js and V8
by amix

2.
About me
• Cofounder and lead developer of Plurk.com
Coded most of the frontend and backend
• Mostly code in Python and JavaScript
But I am proﬁcient in other languages as well (C, Java, Lisp, Lua etc.)
• I have coded since I was 12
24 years old now and still love to program :-)
• Not a node.js expert...
But I ﬁnd it very interesting

4.
Why JavaScript matters
• The pool of JavaScript programmers is huge
and it’s getting bigger
• JavaScript’sbrowsers that support it and allthe mobile
think of all the
distribution is among
the
largest
platforms that support it or will support it... Think of Flash
• Big companies like Adobe, Google, Apple and Microsoft
are spending tons of $ in improving JavaScript
• JavaScript is likely to become one of the
most popular languages
• JavaScript is gaining ground on the backend

5.
What makes V8 special
• V8 JavaScript VM is used in Google Chrome
and is developed by a small Google team in
Denmark. V8 is open-source
• V8 team is led by Lars Bak, one of the
leading VM engineers in the world with 20
years of experience in building VMs
• Lars Bak was the technical lead behind
HotSpot (Sun’s Java VM). HotSpot improved
Java’s performance 20x times
• Before HotSpot Lars Bak worked on a Smalltalk VM

7.
What is node.js?
• A system built on top of V8
• Introduces: IO
- non-blocking
- ability to do system calls
- HTTP libraries
- module system (+ other things)
• The non-blocking nature makes node.js a
good ﬁt for comet and next generation
realtime web-applications
• 8000 lines of C/C++, 2000 lines of Javascript, 14 contributors

8.
Advantages of non-blocking
• nginx: non-blocking apache: threaded
• non-blocking can handle more req. pr. sec
and uses a lot less memory
• comet does not scale at all for threaded servers...
graph source: http://blog.webfaction.com/a-little-holiday-present

11.
Blocking vs. non-blocking
The way to do it The way to do
in most other it in node.js!
languages: puts("Enter your name: ")
gets(function (name) {
puts("Name: " + name)
puts("Enter your name: ") })
var name = gets()
puts("Name: " + name)
node.js design philosophy:
To receive data from disk, network or
another process there must be a callback.

13.
Comet vs. Ajax
Ajax is so yesterday...
Comet is the new superhero on the block
Comet can be used to create real time web-applications
Examples include Plurk and Google Wave. Demo of Plurk real time messaging

15.
Major point
• Comet servers need to have a lot of open
connections
• One thread pr. connection does not scale
• The solution is to use event based servers
• It’s only possible to create event based
servers in node.js!

16.
Implementing comet
• Long polling parts. Used in Plurk
- works for most
- is more expensive and problematic than streaming
• Streaming withoutto proxies and ﬁrewalls
- Very problematic due
WebSockets:
• Streaming with WebSockets (HTML5):side
- Makes comet trivial on both client and server
• In this presentation we will focus on the
future: Building a chat application with
WebSockets and node.js

17.
WebSockets
• Aims to expose TCP/IP sockets to browsers,
while respecting the constraints of the web
(security, proxies and ﬁrewalls)
• A thin layer on top of TCP/IP that adds:
• origin based security model
• Addressing and protocol naming mechanism for supporting multiple
services on one port and multiple host names on one IP address
• framing mechanism for data transporation
• More information: http://dev.w3.org/html5/websockets/
• Currently implemented in Google Chrome

19.
Other comet servers
• JBoss Netty: Java library for doing non-
blocking networking, based on java.nio
used by Plurk to handle 200.000+ open connections
• erlycomet: Erlang comet server based on
MochiWeb
• Tornado: FriendFeed’s Python non-
blocking server
• I have tried most of the popular approaches
and none of them feel as natural as node.js!