I want to write a stream that automaticly adds new posts as they come in. I cant see how to do this with Ajax because i would have to request a script every second to keep it updated, and this would overload my server. Can anyone offer any solutions for this? Thanks :)
-
There are a number of ways of completing the above. Does it need to be done live or can you afford to bring them up in batches? – lethalMango Apr 14 '11 at 15:24
-
The only "real" push that exists in web context is WebSocket protocol - and as far as I know, only Chrome supports the current version of the protocol. Long pooling (or requesting every X seconds) is the only way you can emulate this behavior. – Michael J.V. Apr 14 '11 at 15:40
4 Answers
Overload your server? Are you sure? How many connections you planning on supporting? Make it every 2 seconds and you cut your traffic in half and most likely no one will notice.
Otherwise you're looking at some style of Comet, server side push, with persistent connections to the server.
Refer to:
Long-lived connections (asynchronous server push) with Apache/PHP/Javascript?

- 1
- 1

- 115,893
- 19
- 128
- 203
You can try XMPP
if you want more thing in realtime but if you want only for above requirement then you can try out node.js

- 4,848
- 3
- 35
- 52
You may use the feature of streaming
https://github.com/mojolly/jquery.evented_ajax.js#readme
And actually or better stucture you ajax requests and use some pooling tecniques
http://dev.sencha.com/deploy/ext-4.0-beta2/examples/direct/direct.html

- 2,619
- 6
- 24
- 36
You want Comet long-polling. Comet is so named because like Ajax, it is a brand of detergent.
Comet takes advantage of the fact that browsers can have two open requests to the server. One of these requests is used to push data to the server, say in reaction to UI events happening in the browser.
The other connection is continually connected to the server in a 'long poll'. When the long-polling connection times out or is disconnected, it is automatically re-established by the client (browser).
When the server wants to push data to the client, it immediately writes this data to the waiting long-poll request and ends it, resulting in a more responsive experience than traditional setTimeout()
polling with less overhead.
PHP is not ideal for this. Consider Nodejs.

- 406
- 3
- 11