SlideShare a Scribd company logo
Implement Comet using PHP Dutch PHP Conference 2011 Jonas Mariën – King Foo www.king-foo.be
Jonas Mariën, King Foo www.king-foo.be Author “Zend Framework Web Services” (php|architect) Online since 93 (yep). PHP for fun since 96, professional since 2001. Master in Biological and Genetic Engineering, University of Leuven.
Comet = data push from server to client
Comet aka Reverse Ajax HTTP server Push Ajax Push HTTP streaming ... abused/umbrella term
Use case Example updates being pushed: ticker info
news items
chat messages
status updates
e-mail notifications
in-app messaging
monitoring feedback
feedback on processes that take time to complete
...
Push? But, but ... a browser pulls and the server responds, no? Fake push using polling trickery
Connection is kept open as long as possible
A giant hack
Does the trick for now
The Basics
Short polling Client tries to get something new from the server and polls at regular and short intervals.
Responsiveness at the cost of performance (on the server side). Wasteful in terms of resources.
Easy, but not really what we are looking for.
Not considered 'comet'.
Short polling function  getUpdate()  { $.ajax({ type: "POST", url: "server.php", data: "type=news&category=PHP", success : function(msg){ $('#feed').append(msg);/** we do something **/ setTimeout("getUpdate()", 2000); } }); } $(document).ready(function() { getUpdate(); }
Short polling demo
The forever frame An iframe keeps loading, never stops (or reloads occasionally)
Also called “streaming”
Dirty, e.g IE doesn't like this
The forever frame In the page: <script type=&quot;text/javascript&quot;>function updateResult(/** do something **/)</script>) <iframe style='width: 0px; height: 0px' src='./serverside.php'></iframe> On the server: <?php set_time_limit(0); header(&quot;Cache-Control: no-cache, must-revalidate&quot;); header(&quot;Expires: Mon, 24 Jul 1974 05:00:00 GMT&quot;); flush(); ob_flush();//if ob_start (implicitly) called before while(true) { $result = date('Y-m-d H:i:s'); echo '<script type=&quot;text/javascript&quot;>parent.updateResult(\''.$result.'\');</script>'; flush(); ob_flush(); $sleeptimer = rand(1, 15);  sleep($sleeptimer); }
The forever frame demo
XHR “XHR streaming”
Output 'busy' && sleep() on the server side untill finishing
We need to get  the status before  oncomplete/onsuccess Note: there is also multipart/x-mixed-replace  as part of XHR
XHR “ The XMLHttpRequest object can be in several states.  The readyState attribute must return the current state, which must be one of the following values” * http://www.w3.org/TR/XMLHttpRequest/#states UNSENT (0) The object has been constructed.  OPENED (1) The open() method has been successfully invoked. During this state request headers can be set using setRequestHeader() and the request can be made using the send() method.  HEADERS_RECEIVED (2) All redirects (if any) have been followed and all HTTP headers of the final response have been received. Several response members of the object are now available.  LOADING (3) The response entity body is being received.  DONE (4) The data transfer has been completed or something went wrong during the transfer (e.g. infinite redirects).
XHR Sine jQuery 1.5, no direct access to the underlying XHR object
No access to loading state
Can use a filter: http://bugs.jquery.com/ticket/8327
http://jsfiddle.net/d8ckU/1/ Plugin: http://plugins.jquery.com/project/ajax-http-stream
Mixed results using different browsers for direct XHR manipulation
XHR xhr.onreadystatechange = function() { if(http.readyState == 3  && http.status == 200) { //do something   document.write(xhr.responseText); } }
Long polling Longer intervals
Each time something  is returned from the  server, a new request  is started
Using XHR
Server: checks for updates for a few cycles and keeps a counter. If counter limit reached, 'false' or 'error' message goes back. If an update is available before that, exit the loop and return an 'ok' message with payload.
Long Polling function getUpdate(){ $.ajax({ type: &quot;GET&quot;, url: &quot;server.php&quot;, async: true,  cache: false, timeout:50000, success: function(data) {  /** do something **/ setTimeout ( 'getUpdate()', 1000 ); }, error: function(XMLHttpRequest, textStatus, errorThrown){ /**show error notice**/ setTimeout ( 'getUpdate()', &quot;20000&quot;); }, }); }; $(document).ready(function(){ getUpdate();  });
Long Polling <?php ...  $twitterSearch  = new Zend_Service_Twitter_Search('json'); $hashtag = 'dpc11'; if (isset($_GET['hashtag'])) { $hashtag = $_GET['hashtag']; } $result = false; while($result === false) { if (isset($_GET['since_id']) && $_GET['since_id'] > 0) { $since_id = (int) $_GET['since_id'] + 1; $searchResults  = $twitterSearch->search($hashtag, array('lang' => 'en', 'rpp' => 30,'since_id' => $since_id)); } else { $searchResults  = $twitterSearch->search($hashtag, array('lang' => 'en','rpp' => 30)); } if (isset($searchResults['results']) && count($searchResults['results'])) { $result = json_encode($searchResults['results'][0]); } } echo $result;
Long Polling demo
Thoughts & notes Piggybacking
Long polling issues: Requests are kept open for a while
Objects (other then the ones being pulled/pushed) on server side can be changed, need to have these changes fed to the client. Piggybacking to the rescue again?
One or more long polling requests open and then an AJAX call: might give you browser problems with simultaneous requests
Server performance: we need event driven solutions (message queues and/or something like node.js) to fix this
Proxies (chunking stuff) and firewalls (dropping long connections). Some Comet libs (next slides) try to detect intermediate proxies Comet is not mature. Uses HTTP against most of the basic assumptions and no real standards available. Not a real standard.
Thoughts & Notes Performance is the biggest problem A Comet server should be able to handle as much connections as fast as possible, with a high troughput and low latency.  Long polling requests stay idle for a while, so claiming a thread is not ok.
Frameworks/Toolkits
DWR Direct Web Remoting  (http://directwebremoting.org)
Nice, but Java  only
Fallback different  polling mechanisms
Dynamic Javascript  generation
Attempts at PHP implementations, one could use something like json-rpc to mimic this Could use a Java bridge from PHP perhaps?
Cometd & Bayeux Dojo Foundation
Cometd: reference implementation (in Java, Perl and Python). Java implementation uses Jetty.
Bayeux: protocol, JSON for request/response
Handshake + Publish/Subscribe

More Related Content

PDF
Symfony2 Components - The Event Dispatcher
PPT
Real Time Event Dispatcher
PDF
Beyond Breakpoints: A Tour of Dynamic Analysis
PPT
Assurer - a pluggable server testing/monitoring framework
PDF
Comet with node.js and V8
KEY
Plack - LPW 2009
PPT
How Danga::Socket handles asynchronous processing and how to write asynchrono...
PDF
AnyMQ, Hippie, and the real-time web
Symfony2 Components - The Event Dispatcher
Real Time Event Dispatcher
Beyond Breakpoints: A Tour of Dynamic Analysis
Assurer - a pluggable server testing/monitoring framework
Comet with node.js and V8
Plack - LPW 2009
How Danga::Socket handles asynchronous processing and how to write asynchrono...
AnyMQ, Hippie, and the real-time web

What's hot (20)

PDF
Selenium sandwich-3: Being where you aren't.
PDF
Ruby HTTP clients comparison
ODP
An Introduction to Windows PowerShell
KEY
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
PDF
Choosing a Javascript Framework
PPT
Writing Pluggable Software
KEY
PyCon US 2012 - State of WSGI 2
PDF
Using Websockets with Play!
PDF
Tornado Web Server Internals
PDF
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
PPTX
PowerShell: Automation for everyone
PPTX
Tornado web
PPTX
Python, async web frameworks, and MongoDB
PPTX
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
PDF
Rack Middleware
PDF
"Swoole: double troubles in c", Alexandr Vronskiy
PDF
Nodejs Explained with Examples
PPT
Perlbal Tutorial
PPT
Servlets
PDF
Microservice Teststrategie mit Symfony2
Selenium sandwich-3: Being where you aren't.
Ruby HTTP clients comparison
An Introduction to Windows PowerShell
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Choosing a Javascript Framework
Writing Pluggable Software
PyCon US 2012 - State of WSGI 2
Using Websockets with Play!
Tornado Web Server Internals
High Quality Symfony Bundles tutorial - Dutch PHP Conference 2014
PowerShell: Automation for everyone
Tornado web
Python, async web frameworks, and MongoDB
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Rack Middleware
"Swoole: double troubles in c", Alexandr Vronskiy
Nodejs Explained with Examples
Perlbal Tutorial
Servlets
Microservice Teststrategie mit Symfony2
Ad

Similar to Implementing Comet using PHP (20)

PPT
course slides -- powerpoint
PPT
Eugene Andruszczenko: jQuery
PPT
jQuery Presentation - Refresh Events
PPT
Real-Time Python Web: Gevent and Socket.io
PPT
Java Servlets
PPT
Rapid java backend and api development for mobile devices
PPT
Ajax to the Moon
PDF
Cross Domain Web
Mashups with JQuery and Google App Engine
PPTX
Designing CakePHP plugins for consuming APIs
PDF
Pushing the Web: Interesting things to Know
PPT
En story of cakephp2.0
PPT
Node.js: CAMTA Presentation
PPTX
Spring MVC 3 Restful
PPT
Going crazy with Node.JS and CakePHP
ODP
Web program-peformance-optimization
PPTX
Ruby C10K: High Performance Networking - RubyKaigi '09
PDF
Presto anatomy
PDF
[245] presto 내부구조 파헤치기
ODP
ActiveWeb: Chicago Java User Group Presentation
ODP
Servlets
course slides -- powerpoint
Eugene Andruszczenko: jQuery
jQuery Presentation - Refresh Events
Real-Time Python Web: Gevent and Socket.io
Java Servlets
Rapid java backend and api development for mobile devices
Ajax to the Moon
Cross Domain Web
Mashups with JQuery and Google App Engine
Designing CakePHP plugins for consuming APIs
Pushing the Web: Interesting things to Know
En story of cakephp2.0
Node.js: CAMTA Presentation
Spring MVC 3 Restful
Going crazy with Node.JS and CakePHP
Web program-peformance-optimization
Ruby C10K: High Performance Networking - RubyKaigi '09
Presto anatomy
[245] presto 내부구조 파헤치기
ActiveWeb: Chicago Java User Group Presentation
Servlets
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Empathic Computing: Creating Shared Understanding
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PPTX
Cloud computing and distributed systems.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Big Data Technologies - Introduction.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Encapsulation theory and applications.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Understanding_Digital_Forensics_Presentation.pptx
MYSQL Presentation for SQL database connectivity
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Empathic Computing: Creating Shared Understanding
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Spectral efficient network and resource selection model in 5G networks
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
Cloud computing and distributed systems.
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Big Data Technologies - Introduction.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Encapsulation theory and applications.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

Implementing Comet using PHP