views:

278

answers:

2

I was looking at Twitter Streaming API for getting a real-time feed. But I dont want it stored on my server. I just want it pulled from the server and the browser page will retrieve the data from my server's twitter pull URL. But I want to avoid polling my server every few mill-seconds. Is there a way for my server script to keep pushing to my browser page ?

+2  A: 

Check out COMET.

In web development, Comet is a neologism to describe a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it.


I've always wanted to try this method but I haven't gotten around to it:

Hidden IFrame

A basic technique for dynamic web application is to use a hidden IFrame HTML element (an inline frame, which allows a website to embed one HTML document inside another). This invisible IFrame is sent as a chunked block, which implicitly declares it as infinitely long (sometimes called “forever frame”). As events occur, the iframe is gradually filled with script tags, containing JavaScript to be executed in the browser. Because browsers render HTML pages incrementally, each script tag is executed as it is received.[8]

One benefit of the IFrame method is that it works in every common browser. Two downsides of this technique are the lack of a reliable error handling method, and the impossibility of tracking the state of the request calling process.[8]

David Murdoch
"Hidden IFrame"http://www.obviously.com/tech_tips/slow_load_techniqueI tried this in php and works in FF $i < 10; $i++){ echo $i."<br/>"; flush(); sleep(1);}?></code>
Phonethics
well, IE6 should die. can you post your JS and iframe code?
David Murdoch
Ah, looks like chrome buffers 256 bytes before handing data up the stack. So you will need to chunk (send) at least 256 bytes BEFORE your php code echo()s and flush()es.
David Murdoch
One more thing: the buffer applies to both the (i)frame and XHR (ajax) techniques for COMET.
David Murdoch
+1  A: 

Just how live do you want it? There are ways to set up sockets, but they can be fairly complex, and still consume their fair share of bandwidth.

Is it acceptable to poll every 5, 10 seconds or so? Every few milliseconds would give you pretty darn "live" results, but I would not be upset as a user if it took a matter of seconds for something to appear on your website. That would be satisfactorily "live" for me.

Matchu
I agree. Polling 1 second after the previous request would still be okay. You would just need to send a date/time stamp with each request so your server only replies with the freshest tweets to keep things speedy.
David Murdoch