Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

The only way I can think of to have a shoutbox or similar element update itself when somebody posts a new 'shout' and it needs to be loaded in everyone else's browsers is to have Javascript check every x seconds for any updates... This could get a bit resource intensive though I expect if many people were to leave their browsers open on the page, idling.

Is this the only way or am I missing something?

I've prefer to stick to only html, css, javascript (AJAX) and php.

share|improve this question

closed as too broad by John Conde Oct 14 '13 at 1:23

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
This is not a duplicate of this question but it may have the answer you are looking for (not entirely sure though): stackoverflow.com/questions/732705/… –  joshuahedlund Nov 22 '11 at 21:04

2 Answers 2

For any application where you want instant updates as they're being made, it will be resource intensive. If you went the PHP way, you could make it that updates only showed after a page refresh, but that would frustrate users.

Another better way of doing it is to use the twitter method, where a JS popup shows saying "2 new posts have been made, click to read", this will lessen the consumption of resources because the actual data won't be pulled all the time and will only be pulled upon request by the user.

Of course this won't work for a chat app, which will require continuous rendering and I suggest you look at something like Node.js for programming something like this (there's probably code out there already for a chat app, you just have to implement it).

share|improve this answer

As joshuahedlund's link notes, the traditional way of doing this is through a collection of techniques known as Comet, the most popular of which is long polling, as it's universally supported by browsers. With long polling, you simply make an XHR or JSONP request to the server, which the server does not respond to until it has a new event to report. When the connection times out, the browser automatically makes a new request, and the process is repeated.

However, HTML5 has introduced a new technology called WebSockets, which creates a true persistent connection between browser and server to allow for true server-push messaging. Even though browser support is limited still, you can already use this technology in production through libraries like socket.io, which has cross-browser fallbacks.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.