Implementing Comet using PHP

  • Upload
    king-foo

  • View
    39.191

  • Download
    0

Embed Size (px)

Citation preview

Implement Comet using PHP

Dutch PHP Conference 2011Jonas Marin King Foowww.king-foo.be

Jonas Marin, King Foo www.king-foo.beAuthor 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 AjaxHTTP server PushAjax PushHTTP 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:function updateResult(/** do something **/))

On the server: