SlideShare a Scribd company logo
1 of 75
Download to read offline
Going Live! with Comet
            Simon Willison - http://simonwillison.net/
            SkillSwap Brighton, 11th June 2008




Header image: http://svn.xantus.org/shortbus/trunk/cometd-perl/html/
Live data on the Web
•   A new frontier for web apps

•   Chat, everywhere - Meebo, Facebook, GChat...

•   Twitter (well, it would be nice if it was live)

•   Life-casting, live video

•   Collaboration tools

•   Push, not Pull
http://www.engadget.com/2008/06/09/iphone-push-notification-service-for-devs-announced/
Comet: any method that
allows a web server to
 push events “live” to
 connected browsers
A new name for a very
     old technique
• Also known as...
 • HTTP push
 • Server push
 • Reverse Ajax
 • Pushlets
 • ...
Coined by Alex Russell in 2006
Deliberately named after a kitchen cleaner
What can you do with it?
Google Finance
Early Comet
Netscape 1.1, March 1995
Netscape 1.1 new features
Netscape 1.1 new features

• Tables
Netscape 1.1 new features

• Tables
• Background images
Netscape 1.1 new features

• Tables
• Background images
• “Dynamic Documents”
Netscape 1.1 new features

• Tables
• Background images
• “Dynamic Documents”
 • Client Pull
Netscape 1.1 new features

• Tables
• Background images
• “Dynamic Documents”
 • Client Pull
 • Server Push
Netscape 1.1 new features

• Tables
• Background images
• “Dynamic Documents”
 • Client Pull
 • Server Push
• Predates JavaScript, Internet Explorer and Flash
Server Push, Client Pull
“Client Pull”


<META HTTP-EQUIV=quot;Refreshquot; CONTENT=1>
“Server Push”
Content-type: multipart/x-mixed-replace;boundary=XXooXX

--XXooXX
Content-type: text/plain

Data for the first object.

--XXooXX
Content-type: text/plain

Data for the second and last object.

--XXooXX--
http://zesty.ca/chat/ (1999)
Modern Comet
var xhr = new XMLHttpRequest();
xhr.open('GET', '/comet', true);

xhr.onreadystatechange = function() {
    if (readyState == 3) {
        // Process xhr.responseText
    }
};
If only it were
 that simple...
An
Epic Saga
Of geek v.s. browser




                       http://airminded.org/2007/05/25/the-movie-that-time-forgot/
Problem #1
Problem #1
Problem #1




xhr.responseText isn’t available until
    the page has finished loading
A hidden iframe
         (The forever frame technique)


• Add a hidden iframe pointing at a streaming
  page
• Send down chunks of <script> blocks, which
  the browser will execute as part of
  progressive rendering
• Add 1KB of junk at the start to trigger
  progressive rendering in IE
Problem #2
             The throbber!
Problem #2
             The throbber!



Problem #3     The click!
Solved for GChat
var htmlfile = new ActiveXObject('htmlfile');


• An undocumented ActiveX extension
 • Create an quot;htmlfilequot; instance
 • Create an iframe inside it
 • Set up some cross-frame scripting hooks
 • Run comet using the iframe
• No clicks! No throbber!
Confuse IE with enough nested
 iframes and it forgets to click


                  Huh?
Problem #4
Problem #4




   Proxies!
Long polling

•   Make a request (iframe, XHR or even a <script> tag)

•   Server pretends to be processing it really slowly...

•   An event occurs; the server finally returns the response

•   The browser starts a new request straight away
Problem #5

The two connection limit
• mysite.com - regular site
• comet.mysite.com - comet server
• Cross-domain communication is easy - just add
  another iframe and set document.domain to
  enable cross-frame scripting

• (Then solve the resulting browser history
  problems with a few more iframes)

• Facebook go a step further and use incrementing
  subdomains for every request
• mysite.com - regular site
• comet.mysite.com - comet server
• Cross-domain communication is easy - just add
  another iframe and set document.domain to
  enable cross-frame scripting

• (Then solve the resulting browser history
  problems with a few more iframes)

• Facebook go a step further and use incrementing
  subdomains for every request
• mysite.com - regular site
• comet.mysite.com - comet server
• Cross-domain communication is easy - just add
  another iframe and set document.domain to
  enable cross-frame scripting

• (Then solve the resulting browser history
  problems with a few more iframes)

• Facebook go a step further and use incrementing
  subdomains for every request
And if all else fails...
Are we there yet?
                                            Are we there yet?
                                            Are we there yet?




Fall back to polling
  SoundAndFury http://www.flickr.com/photos/sound_and_fury/2166186492/
Pterodactyls win!
Key techniques

• Streaming (ideal)
 • XMLHttpRequest
 • Forever frame (for IE)
• Long polling (works through proxies)
• Polling (if all else fails)
Future Comet
HTML 5 event-source
http://www.whatwg.org/specs/web-apps/current-work/#event-source


<event-source src=quot;/cometquot;>



Content-Type: application/x-dom-event-stream

Event: server-time
data: [time on the server]



Support added in Opera 9.5
Google Gears 0.2
“Wow, client-side
   Comet sucks”...
that’s not the half of it
Scaling the server
  http://flickr.com/photos/adrianblack/254968866/ craig1black
• Comet requires potentially tens of
  thousands of simultaneous HTTP
  connections

• Apache and other mainstream servers are
  designed to handle a single request as
  quickly as possible

  • Requests are generally served by a worker
    process or thread

• This absolutely will not scale to Comet
Event-based IO
•   Alternative architecture designed to handle
    thousands of simultaneous connections
    •   Twisted (Python)
    •   Jetty (Java)
    •   Perlbal
    •   Orbited
    •   Meteor
    •   Facebook used MochiWeb (in Erlang)
Bayeaux
Bayeaux
•   Bayeaux is a protocol for Comet

•   Any Bayeaux client can talk to any Bayeaux server

•   The protocol is based on clients publishing and
    subscribing to channels

•   Data is encoded using JSON

•   Clients can create a permanent connection using a
    handshake, or send simple one-off messages
Dumb Comet servers
• The principle advantage of Bayeaux is that
  your Comet server can be stupid - it
  doesn’t need any application logic, it just
  needs to route messages around
  • It’s a black box which you simply drop in
    to your architecture
• This means your application logic can stay on
  your favourite platform
Cometd
• Cometd (Comet Daemon) is an umbrella
  project for a number of Bayeaux
  implementations
 • cometd-python (in Twisted)
 • cometd-perl (also a Perlbal plugin)
 • cometd-java (on Jetty)
 • dojox.cometd (JavaScript client)
• http://cometd.com/
So despite all of that...
 Comet applications
  are easy to build
How to build a Comet
application (in 5 minutes)
Set up Jetty

•   Download and unzip Jetty-6.1 from www.mortbay.org

•   Install Maven from http://maven.apache.org/

•   cd jetty-6.1.6/contrib/cometd/demo/

•   mvn jetty:run

•   Browse to http://localhost:8080/

•   Mess around with the JS in src/main/webapp/examples/
dojox.cometd
dojo.addOnLoad(function() {
    dojox.cometd.init(quot;http://127.0.0.1:8080/cometdquot;);
    dojox.cometd.subscribe(quot;/mychannelquot;, function(comet) {
        alert(comet.data); // a JSON object
    });

      dojo.byId('send').onclick = function() {
          dojox.cometd.publish(quot;/mychannelquot;, {
              'msg': quot;A message sent by Cometquot;
          });
          return false;
      };
});
dojox.cometd
dojo.addOnLoad(function() {
    dojox.cometd.init(quot;http://127.0.0.1:8080/cometdquot;);
    dojox.cometd.subscribe(quot;/mychannelquot;, function(comet) {
        alert(comet.data); // a JSON object
    });

      dojo.byId('send').onclick = function() {
          dojox.cometd.publish(quot;/mychannelquot;, {
              'msg': quot;A message sent by Cometquot;
          });
          return false;
      };
});
A slideshow in 5 lines of code

<script type=quot;text/javascriptquot;>
dojo.require(quot;dojox.cometdquot;);
jQuery(function($) {
    dojox.cometd.init(quot;http://127.0.0.1:8080/cometdquot;);
    dojox.cometd.subscribe(quot;/slideshow/changequot;, function(comet) {
        $('#currentSlide').attr('src', comet.data.src);
    });
});
</script>
Conclusions

• Comet is possible, therefore it’s inevitable
• It requires hacks, but they’re reasonably well
  abstracted and understood
• Bayeaux and Cometd abstract away the
  nastiness and make Comet accessible to
  sane developers
More crazy hacks...
• http://meteorserver.org/browser-techniques/
• http://cometdaily.com/2007/10/25/http-
  streaming-and-internet-explorer/
• http://cometdaily.com/2007/11/18/ie-
  activexhtmlfile-transport-part-ii/
• http://orbited.org/svn/orbit/trunk/daemon/
  orbited/static/orbited.js
• http://simonwillison.net/tags/comet/
Thank you

More Related Content

What's hot

WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)Ericom Software
 
Introduction to WebSockets
Introduction to WebSocketsIntroduction to WebSockets
Introduction to WebSocketsGunnar Hillert
 
Tips for going fast in a slow world: Michael May at OSCON 2015
Tips for going fast in a slow world: Michael May at OSCON 2015Tips for going fast in a slow world: Michael May at OSCON 2015
Tips for going fast in a slow world: Michael May at OSCON 2015Fastly
 
Building Real-Time Applications with Android and WebSockets
Building Real-Time Applications with Android and WebSocketsBuilding Real-Time Applications with Android and WebSockets
Building Real-Time Applications with Android and WebSocketsSergi Almar i Graupera
 
0-60 with Goliath: Building High Performance Ruby Web-Services
0-60 with Goliath: Building High Performance Ruby Web-Services0-60 with Goliath: Building High Performance Ruby Web-Services
0-60 with Goliath: Building High Performance Ruby Web-ServicesIlya Grigorik
 
Ruby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.com
Ruby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.comRuby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.com
Ruby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.comIlya Grigorik
 
Building Realtime Apps with Ember.js and WebSockets
Building Realtime Apps with Ember.js and WebSocketsBuilding Realtime Apps with Ember.js and WebSockets
Building Realtime Apps with Ember.js and WebSocketsBen Limmer
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with javaJeongHun Byeon
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketMauricio "Maltron" Leal
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serializationGWTcon
 
Ruby C10K: High Performance Networking - RubyKaigi '09
Ruby C10K: High Performance Networking - RubyKaigi '09Ruby C10K: High Performance Networking - RubyKaigi '09
Ruby C10K: High Performance Networking - RubyKaigi '09Ilya Grigorik
 
vlavrynovych - WebSockets Presentation
vlavrynovych - WebSockets Presentationvlavrynovych - WebSockets Presentation
vlavrynovych - WebSockets PresentationVolodymyr Lavrynovych
 
Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Arun Gupta
 
Unity and WebSockets
Unity and WebSocketsUnity and WebSockets
Unity and WebSocketsJosh Glover
 
Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011David Troy
 
Stupid Boot Tricks: using ipxe and chef to get to boot management bliss
Stupid Boot Tricks: using ipxe and chef to get to boot management blissStupid Boot Tricks: using ipxe and chef to get to boot management bliss
Stupid Boot Tricks: using ipxe and chef to get to boot management blissmacslide
 
0-60 with Goliath: High performance web services
0-60 with Goliath: High performance web services0-60 with Goliath: High performance web services
0-60 with Goliath: High performance web servicesIlya Grigorik
 

What's hot (20)

WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
WebSockets Everywhere: the Future Transport Protocol for Everything (Almost)
 
Introduction to WebSockets
Introduction to WebSocketsIntroduction to WebSockets
Introduction to WebSockets
 
Intro to WebSockets
Intro to WebSocketsIntro to WebSockets
Intro to WebSockets
 
Tips for going fast in a slow world: Michael May at OSCON 2015
Tips for going fast in a slow world: Michael May at OSCON 2015Tips for going fast in a slow world: Michael May at OSCON 2015
Tips for going fast in a slow world: Michael May at OSCON 2015
 
Building Real-Time Applications with Android and WebSockets
Building Real-Time Applications with Android and WebSocketsBuilding Real-Time Applications with Android and WebSockets
Building Real-Time Applications with Android and WebSockets
 
0-60 with Goliath: Building High Performance Ruby Web-Services
0-60 with Goliath: Building High Performance Ruby Web-Services0-60 with Goliath: Building High Performance Ruby Web-Services
0-60 with Goliath: Building High Performance Ruby Web-Services
 
Ruby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.com
Ruby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.comRuby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.com
Ruby Proxies for Scale, Performance, and Monitoring - GoGaRuCo - igvita.com
 
Building Realtime Apps with Ember.js and WebSockets
Building Realtime Apps with Ember.js and WebSocketsBuilding Realtime Apps with Ember.js and WebSockets
Building Realtime Apps with Ember.js and WebSockets
 
Realtime web application with java
Realtime web application with javaRealtime web application with java
Realtime web application with java
 
Enhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocketEnhancing Mobile User Experience with WebSocket
Enhancing Mobile User Experience with WebSocket
 
GWT Web Socket and data serialization
GWT Web Socket and data serializationGWT Web Socket and data serialization
GWT Web Socket and data serialization
 
Ruby C10K: High Performance Networking - RubyKaigi '09
Ruby C10K: High Performance Networking - RubyKaigi '09Ruby C10K: High Performance Networking - RubyKaigi '09
Ruby C10K: High Performance Networking - RubyKaigi '09
 
vlavrynovych - WebSockets Presentation
vlavrynovych - WebSockets Presentationvlavrynovych - WebSockets Presentation
vlavrynovych - WebSockets Presentation
 
Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014Nuts and Bolts of WebSocket Devoxx 2014
Nuts and Bolts of WebSocket Devoxx 2014
 
Unity and WebSockets
Unity and WebSocketsUnity and WebSockets
Unity and WebSockets
 
Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011Servers with Event Machine - David Troy - RailsConf 2011
Servers with Event Machine - David Troy - RailsConf 2011
 
Reverse Ajax
Reverse AjaxReverse Ajax
Reverse Ajax
 
Php push notifications
Php push notificationsPhp push notifications
Php push notifications
 
Stupid Boot Tricks: using ipxe and chef to get to boot management bliss
Stupid Boot Tricks: using ipxe and chef to get to boot management blissStupid Boot Tricks: using ipxe and chef to get to boot management bliss
Stupid Boot Tricks: using ipxe and chef to get to boot management bliss
 
0-60 with Goliath: High performance web services
0-60 with Goliath: High performance web services0-60 with Goliath: High performance web services
0-60 with Goliath: High performance web services
 

Similar to Going Live! with Comet

Comet: Making The Web a 2-Way Medium
Comet: Making The Web a 2-Way MediumComet: Making The Web a 2-Way Medium
Comet: Making The Web a 2-Way MediumJoe Walker
 
WebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoT
WebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoTWebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoT
WebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoTFrank Greco
 
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008Association Paris-Web
 
Comet: an Overview and a New Solution Called Jabbify
Comet: an Overview and a New Solution Called JabbifyComet: an Overview and a New Solution Called Jabbify
Comet: an Overview and a New Solution Called JabbifyBrian Moschel
 
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2Asher Martin
 
Performance Improvements In Browsers
Performance Improvements In BrowsersPerformance Improvements In Browsers
Performance Improvements In BrowsersGoogleTecTalks
 
Performance Improvements in Browsers
Performance Improvements in BrowsersPerformance Improvements in Browsers
Performance Improvements in Browsersjeresig
 
Ajax World Comet Talk
Ajax World Comet TalkAjax World Comet Talk
Ajax World Comet Talkrajivmordani
 
Comet from JavaOne 2008
Comet from JavaOne 2008Comet from JavaOne 2008
Comet from JavaOne 2008Joe Walker
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSocketsGonzalo Ayuso
 
Rob Tweed :: Ajax and the Impact on Caché and Similar Technologies
Rob Tweed :: Ajax and the Impact on Caché and Similar TechnologiesRob Tweed :: Ajax and the Impact on Caché and Similar Technologies
Rob Tweed :: Ajax and the Impact on Caché and Similar Technologiesgeorge.james
 
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Patrick Chanezon
 
No Callbacks, No Threads - RailsConf 2010
No Callbacks, No Threads - RailsConf 2010No Callbacks, No Threads - RailsConf 2010
No Callbacks, No Threads - RailsConf 2010Ilya Grigorik
 
Toster - Understanding the Rails Web Model and Scalability Options
Toster - Understanding the Rails Web Model and Scalability OptionsToster - Understanding the Rails Web Model and Scalability Options
Toster - Understanding the Rails Web Model and Scalability OptionsFabio Akita
 
Understanding the Rails web model and scalability options
Understanding the Rails web model and scalability optionsUnderstanding the Rails web model and scalability options
Understanding the Rails web model and scalability options.toster
 

Similar to Going Live! with Comet (20)

Time for Comet?
Time for Comet?Time for Comet?
Time for Comet?
 
Grizzly Comet Aquarium Paris
Grizzly Comet Aquarium ParisGrizzly Comet Aquarium Paris
Grizzly Comet Aquarium Paris
 
Comet: Making The Web a 2-Way Medium
Comet: Making The Web a 2-Way MediumComet: Making The Web a 2-Way Medium
Comet: Making The Web a 2-Way Medium
 
WebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoT
WebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoTWebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoT
WebSocket Perspectives 2015 - Clouds, Streams, Microservices and WoT
 
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
IE 8 et les standards du Web - Chris Wilson - Paris Web 2008
 
Comet: an Overview and a New Solution Called Jabbify
Comet: an Overview and a New Solution Called JabbifyComet: an Overview and a New Solution Called Jabbify
Comet: an Overview and a New Solution Called Jabbify
 
Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2Cape Cod Web Technology Meetup - 2
Cape Cod Web Technology Meetup - 2
 
Performance Improvements In Browsers
Performance Improvements In BrowsersPerformance Improvements In Browsers
Performance Improvements In Browsers
 
Performance Improvements in Browsers
Performance Improvements in BrowsersPerformance Improvements in Browsers
Performance Improvements in Browsers
 
Ajax World Comet Talk
Ajax World Comet TalkAjax World Comet Talk
Ajax World Comet Talk
 
20090315 Comet
20090315 Comet20090315 Comet
20090315 Comet
 
Comet from JavaOne 2008
Comet from JavaOne 2008Comet from JavaOne 2008
Comet from JavaOne 2008
 
Nodejs and WebSockets
Nodejs and WebSocketsNodejs and WebSockets
Nodejs and WebSockets
 
Grizzly 20080925 V2
Grizzly 20080925 V2Grizzly 20080925 V2
Grizzly 20080925 V2
 
Rob Tweed :: Ajax and the Impact on Caché and Similar Technologies
Rob Tweed :: Ajax and the Impact on Caché and Similar TechnologiesRob Tweed :: Ajax and the Impact on Caché and Similar Technologies
Rob Tweed :: Ajax and the Impact on Caché and Similar Technologies
 
Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?Google's HTML5 Work: what's next?
Google's HTML5 Work: what's next?
 
ICEfaces and JSF 2.0 on GlassFish
ICEfaces and JSF 2.0 on GlassFishICEfaces and JSF 2.0 on GlassFish
ICEfaces and JSF 2.0 on GlassFish
 
No Callbacks, No Threads - RailsConf 2010
No Callbacks, No Threads - RailsConf 2010No Callbacks, No Threads - RailsConf 2010
No Callbacks, No Threads - RailsConf 2010
 
Toster - Understanding the Rails Web Model and Scalability Options
Toster - Understanding the Rails Web Model and Scalability OptionsToster - Understanding the Rails Web Model and Scalability Options
Toster - Understanding the Rails Web Model and Scalability Options
 
Understanding the Rails web model and scalability options
Understanding the Rails web model and scalability optionsUnderstanding the Rails web model and scalability options
Understanding the Rails web model and scalability options
 

More from Simon Willison

Cheap tricks for startups
Cheap tricks for startupsCheap tricks for startups
Cheap tricks for startupsSimon Willison
 
The Django Web Framework (EuroPython 2006)
The Django Web Framework (EuroPython 2006)The Django Web Framework (EuroPython 2006)
The Django Web Framework (EuroPython 2006)Simon Willison
 
How we bootstrapped Lanyrd using Twitter's social graph
How we bootstrapped Lanyrd using Twitter's social graphHow we bootstrapped Lanyrd using Twitter's social graph
How we bootstrapped Lanyrd using Twitter's social graphSimon Willison
 
Web Services for Fun and Profit
Web Services for Fun and ProfitWeb Services for Fun and Profit
Web Services for Fun and ProfitSimon Willison
 
Tricks & challenges developing a large Django application
Tricks & challenges developing a large Django applicationTricks & challenges developing a large Django application
Tricks & challenges developing a large Django applicationSimon Willison
 
Advanced Aspects of the Django Ecosystem: Haystack, Celery & Fabric
Advanced Aspects of the Django Ecosystem: Haystack, Celery & FabricAdvanced Aspects of the Django Ecosystem: Haystack, Celery & Fabric
Advanced Aspects of the Django Ecosystem: Haystack, Celery & FabricSimon Willison
 
How Lanyrd uses Twitter
How Lanyrd uses TwitterHow Lanyrd uses Twitter
How Lanyrd uses TwitterSimon Willison
 
Building Things Fast - and getting approval
Building Things Fast - and getting approvalBuilding Things Fast - and getting approval
Building Things Fast - and getting approvalSimon Willison
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesSimon Willison
 
Building crowdsourcing applications
Building crowdsourcing applicationsBuilding crowdsourcing applications
Building crowdsourcing applicationsSimon Willison
 
Evented I/O based web servers, explained using bunnies
Evented I/O based web servers, explained using bunniesEvented I/O based web servers, explained using bunnies
Evented I/O based web servers, explained using bunniesSimon Willison
 
Cowboy development with Django
Cowboy development with DjangoCowboy development with Django
Cowboy development with DjangoSimon Willison
 
Crowdsourcing with Django
Crowdsourcing with DjangoCrowdsourcing with Django
Crowdsourcing with DjangoSimon Willison
 
Class-based views with Django
Class-based views with DjangoClass-based views with Django
Class-based views with DjangoSimon Willison
 
Web App Security Horror Stories
Web App Security Horror StoriesWeb App Security Horror Stories
Web App Security Horror StoriesSimon Willison
 
Web Security Horror Stories
Web Security Horror StoriesWeb Security Horror Stories
Web Security Horror StoriesSimon Willison
 

More from Simon Willison (20)

How Lanyrd does Geo
How Lanyrd does GeoHow Lanyrd does Geo
How Lanyrd does Geo
 
Cheap tricks for startups
Cheap tricks for startupsCheap tricks for startups
Cheap tricks for startups
 
The Django Web Framework (EuroPython 2006)
The Django Web Framework (EuroPython 2006)The Django Web Framework (EuroPython 2006)
The Django Web Framework (EuroPython 2006)
 
Building Lanyrd
Building LanyrdBuilding Lanyrd
Building Lanyrd
 
How we bootstrapped Lanyrd using Twitter's social graph
How we bootstrapped Lanyrd using Twitter's social graphHow we bootstrapped Lanyrd using Twitter's social graph
How we bootstrapped Lanyrd using Twitter's social graph
 
Web Services for Fun and Profit
Web Services for Fun and ProfitWeb Services for Fun and Profit
Web Services for Fun and Profit
 
Tricks & challenges developing a large Django application
Tricks & challenges developing a large Django applicationTricks & challenges developing a large Django application
Tricks & challenges developing a large Django application
 
Advanced Aspects of the Django Ecosystem: Haystack, Celery & Fabric
Advanced Aspects of the Django Ecosystem: Haystack, Celery & FabricAdvanced Aspects of the Django Ecosystem: Haystack, Celery & Fabric
Advanced Aspects of the Django Ecosystem: Haystack, Celery & Fabric
 
How Lanyrd uses Twitter
How Lanyrd uses TwitterHow Lanyrd uses Twitter
How Lanyrd uses Twitter
 
ScaleFail
ScaleFailScaleFail
ScaleFail
 
Building Things Fast - and getting approval
Building Things Fast - and getting approvalBuilding Things Fast - and getting approval
Building Things Fast - and getting approval
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
 
Building crowdsourcing applications
Building crowdsourcing applicationsBuilding crowdsourcing applications
Building crowdsourcing applications
 
Evented I/O based web servers, explained using bunnies
Evented I/O based web servers, explained using bunniesEvented I/O based web servers, explained using bunnies
Evented I/O based web servers, explained using bunnies
 
Cowboy development with Django
Cowboy development with DjangoCowboy development with Django
Cowboy development with Django
 
Crowdsourcing with Django
Crowdsourcing with DjangoCrowdsourcing with Django
Crowdsourcing with Django
 
Django Heresies
Django HeresiesDjango Heresies
Django Heresies
 
Class-based views with Django
Class-based views with DjangoClass-based views with Django
Class-based views with Django
 
Web App Security Horror Stories
Web App Security Horror StoriesWeb App Security Horror Stories
Web App Security Horror Stories
 
Web Security Horror Stories
Web Security Horror StoriesWeb Security Horror Stories
Web Security Horror Stories
 

Recently uploaded

Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI AgeCprime
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPathCommunity
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 

Recently uploaded (20)

Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
A Framework for Development in the AI Age
A Framework for Development in the AI AgeA Framework for Development in the AI Age
A Framework for Development in the AI Age
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
UiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to HeroUiPath Community: Communication Mining from Zero to Hero
UiPath Community: Communication Mining from Zero to Hero
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 

Going Live! with Comet

  • 1. Going Live! with Comet Simon Willison - http://simonwillison.net/ SkillSwap Brighton, 11th June 2008 Header image: http://svn.xantus.org/shortbus/trunk/cometd-perl/html/
  • 2.
  • 3. Live data on the Web • A new frontier for web apps • Chat, everywhere - Meebo, Facebook, GChat... • Twitter (well, it would be nice if it was live) • Life-casting, live video • Collaboration tools • Push, not Pull
  • 5. Comet: any method that allows a web server to push events “live” to connected browsers
  • 6. A new name for a very old technique • Also known as... • HTTP push • Server push • Reverse Ajax • Pushlets • ...
  • 7. Coined by Alex Russell in 2006
  • 8. Deliberately named after a kitchen cleaner
  • 9. What can you do with it?
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 17.
  • 19.
  • 21. Netscape 1.1 new features
  • 22. Netscape 1.1 new features • Tables
  • 23. Netscape 1.1 new features • Tables • Background images
  • 24. Netscape 1.1 new features • Tables • Background images • “Dynamic Documents”
  • 25. Netscape 1.1 new features • Tables • Background images • “Dynamic Documents” • Client Pull
  • 26. Netscape 1.1 new features • Tables • Background images • “Dynamic Documents” • Client Pull • Server Push
  • 27. Netscape 1.1 new features • Tables • Background images • “Dynamic Documents” • Client Pull • Server Push • Predates JavaScript, Internet Explorer and Flash
  • 30. “Server Push” Content-type: multipart/x-mixed-replace;boundary=XXooXX --XXooXX Content-type: text/plain Data for the first object. --XXooXX Content-type: text/plain Data for the second and last object. --XXooXX--
  • 31.
  • 34. var xhr = new XMLHttpRequest(); xhr.open('GET', '/comet', true); xhr.onreadystatechange = function() { if (readyState == 3) { // Process xhr.responseText } };
  • 35. If only it were that simple...
  • 36. An Epic Saga Of geek v.s. browser http://airminded.org/2007/05/25/the-movie-that-time-forgot/
  • 39. Problem #1 xhr.responseText isn’t available until the page has finished loading
  • 40. A hidden iframe (The forever frame technique) • Add a hidden iframe pointing at a streaming page • Send down chunks of <script> blocks, which the browser will execute as part of progressive rendering • Add 1KB of junk at the start to trigger progressive rendering in IE
  • 41. Problem #2 The throbber!
  • 42. Problem #2 The throbber! Problem #3 The click!
  • 43. Solved for GChat var htmlfile = new ActiveXObject('htmlfile'); • An undocumented ActiveX extension • Create an quot;htmlfilequot; instance • Create an iframe inside it • Set up some cross-frame scripting hooks • Run comet using the iframe • No clicks! No throbber!
  • 44. Confuse IE with enough nested iframes and it forgets to click Huh?
  • 46. Problem #4 Proxies!
  • 47. Long polling • Make a request (iframe, XHR or even a <script> tag) • Server pretends to be processing it really slowly... • An event occurs; the server finally returns the response • The browser starts a new request straight away
  • 48. Problem #5 The two connection limit
  • 49. • mysite.com - regular site • comet.mysite.com - comet server • Cross-domain communication is easy - just add another iframe and set document.domain to enable cross-frame scripting • (Then solve the resulting browser history problems with a few more iframes) • Facebook go a step further and use incrementing subdomains for every request
  • 50. • mysite.com - regular site • comet.mysite.com - comet server • Cross-domain communication is easy - just add another iframe and set document.domain to enable cross-frame scripting • (Then solve the resulting browser history problems with a few more iframes) • Facebook go a step further and use incrementing subdomains for every request
  • 51. • mysite.com - regular site • comet.mysite.com - comet server • Cross-domain communication is easy - just add another iframe and set document.domain to enable cross-frame scripting • (Then solve the resulting browser history problems with a few more iframes) • Facebook go a step further and use incrementing subdomains for every request
  • 52. And if all else fails...
  • 53. Are we there yet? Are we there yet? Are we there yet? Fall back to polling SoundAndFury http://www.flickr.com/photos/sound_and_fury/2166186492/
  • 55. Key techniques • Streaming (ideal) • XMLHttpRequest • Forever frame (for IE) • Long polling (works through proxies) • Polling (if all else fails)
  • 57. HTML 5 event-source http://www.whatwg.org/specs/web-apps/current-work/#event-source <event-source src=quot;/cometquot;> Content-Type: application/x-dom-event-stream Event: server-time data: [time on the server] Support added in Opera 9.5
  • 59. “Wow, client-side Comet sucks”... that’s not the half of it
  • 60. Scaling the server http://flickr.com/photos/adrianblack/254968866/ craig1black
  • 61. • Comet requires potentially tens of thousands of simultaneous HTTP connections • Apache and other mainstream servers are designed to handle a single request as quickly as possible • Requests are generally served by a worker process or thread • This absolutely will not scale to Comet
  • 62. Event-based IO • Alternative architecture designed to handle thousands of simultaneous connections • Twisted (Python) • Jetty (Java) • Perlbal • Orbited • Meteor • Facebook used MochiWeb (in Erlang)
  • 64. Bayeaux • Bayeaux is a protocol for Comet • Any Bayeaux client can talk to any Bayeaux server • The protocol is based on clients publishing and subscribing to channels • Data is encoded using JSON • Clients can create a permanent connection using a handshake, or send simple one-off messages
  • 65. Dumb Comet servers • The principle advantage of Bayeaux is that your Comet server can be stupid - it doesn’t need any application logic, it just needs to route messages around • It’s a black box which you simply drop in to your architecture • This means your application logic can stay on your favourite platform
  • 66. Cometd • Cometd (Comet Daemon) is an umbrella project for a number of Bayeaux implementations • cometd-python (in Twisted) • cometd-perl (also a Perlbal plugin) • cometd-java (on Jetty) • dojox.cometd (JavaScript client) • http://cometd.com/
  • 67. So despite all of that... Comet applications are easy to build
  • 68. How to build a Comet application (in 5 minutes)
  • 69. Set up Jetty • Download and unzip Jetty-6.1 from www.mortbay.org • Install Maven from http://maven.apache.org/ • cd jetty-6.1.6/contrib/cometd/demo/ • mvn jetty:run • Browse to http://localhost:8080/ • Mess around with the JS in src/main/webapp/examples/
  • 70. dojox.cometd dojo.addOnLoad(function() { dojox.cometd.init(quot;http://127.0.0.1:8080/cometdquot;); dojox.cometd.subscribe(quot;/mychannelquot;, function(comet) { alert(comet.data); // a JSON object }); dojo.byId('send').onclick = function() { dojox.cometd.publish(quot;/mychannelquot;, { 'msg': quot;A message sent by Cometquot; }); return false; }; });
  • 71. dojox.cometd dojo.addOnLoad(function() { dojox.cometd.init(quot;http://127.0.0.1:8080/cometdquot;); dojox.cometd.subscribe(quot;/mychannelquot;, function(comet) { alert(comet.data); // a JSON object }); dojo.byId('send').onclick = function() { dojox.cometd.publish(quot;/mychannelquot;, { 'msg': quot;A message sent by Cometquot; }); return false; }; });
  • 72. A slideshow in 5 lines of code <script type=quot;text/javascriptquot;> dojo.require(quot;dojox.cometdquot;); jQuery(function($) { dojox.cometd.init(quot;http://127.0.0.1:8080/cometdquot;); dojox.cometd.subscribe(quot;/slideshow/changequot;, function(comet) { $('#currentSlide').attr('src', comet.data.src); }); }); </script>
  • 73. Conclusions • Comet is possible, therefore it’s inevitable • It requires hacks, but they’re reasonably well abstracted and understood • Bayeaux and Cometd abstract away the nastiness and make Comet accessible to sane developers
  • 74. More crazy hacks... • http://meteorserver.org/browser-techniques/ • http://cometdaily.com/2007/10/25/http- streaming-and-internet-explorer/ • http://cometdaily.com/2007/11/18/ie- activexhtmlfile-transport-part-ii/ • http://orbited.org/svn/orbit/trunk/daemon/ orbited/static/orbited.js • http://simonwillison.net/tags/comet/