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

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Recently uploaded (20)

Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
Transcript: #StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

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/