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-notiļ¬cation-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 ļ¬rst 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 ļ¬nished 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;htmlļ¬lequot; 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 ļ¬nally 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.ļ¬‚ickr.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://ļ¬‚ickr.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-
  activexhtmlļ¬le-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

Intro to WebSockets
Intro to WebSocketsIntro to WebSockets
Intro to WebSockets
Gaurav Oberoi
Ā 
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
Ilya Grigorik
Ā 
vlavrynovych - WebSockets Presentation
vlavrynovych - WebSockets Presentationvlavrynovych - WebSockets Presentation
vlavrynovych - WebSockets Presentation
Volodymyr Lavrynovych
Ā 
Unity and WebSockets
Unity and WebSocketsUnity and WebSockets
Unity and WebSockets
Josh Glover
Ā 
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
Ilya 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

Performance Improvements In Browsers
Performance Improvements In BrowsersPerformance Improvements In Browsers
Performance Improvements In Browsers
GoogleTecTalks
Ā 
Ajax World Comet Talk
Ajax World Comet TalkAjax World Comet Talk
Ajax World Comet Talk
rajivmordani
Ā 
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
george.james
Ā 
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

Building Things Fast - and getting approval
Building Things Fast - and getting approvalBuilding Things Fast - and getting approval
Building Things Fast - and getting approval
Simon 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 Libraries
Simon 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

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
Ā 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
Ā 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
Ā 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
Ā 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
Ā 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
Ā 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Ā 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
Ā 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
Ā 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
Ā 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
Ā 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Ā 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Ā 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
Ā 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Ā 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Ā 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
Ā 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Ā 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Ā 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Ā 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
Ā 

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 ļ¬rst 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 ļ¬nished 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;htmlļ¬lequot; 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 ļ¬nally 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.ļ¬‚ickr.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://ļ¬‚ickr.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- activexhtmlļ¬le-transport-part-ii/ ā€¢ http://orbited.org/svn/orbit/trunk/daemon/ orbited/static/orbited.js ā€¢ http://simonwillison.net/tags/comet/