How should I implement "interactive refresh" (I don't even know how I should call it) where one visitor's action (klicking a link, submit button etc. - everything will do) activates page refreshing of other visitors on the same page?

I know how to implement an automatic, periodic refresh but I would like it to refresh only when someone has really made a change to the page (it would be kind of interactive multiuser application).


If it's not possible with php, is there some other way?


- Lennart

Recommended Answers

All 7 Replies

Umm.. are you asking whether it is possible to refresh other visitor's page when 'one' of the visitor clicks a link/submits a form/.. ??

Yes, that's the idea. Or "when ANY of the visitors of the same page clicks a link/button" to be exact. Am I crazy when even dreaming of something like that :-]

Umm..I dont think its possible(I am quite sure its not possible!).. When a user opens a page on your site, hes opening an instance of your page in his browser and its different for different users. Why do you want something like this btw ?

Why would you need something like this?

Building a stateful web application is possible, but not feasible for most application scenarios. As for why a person may want such a thing? LOTS of reasons. An obvious example is a browser-based chat application. You want all members of the chat to see all messages entered as close to real-time as possible.

The most common and easy to implement solution is to build an "AJAX" solution. If you don't know what AJAX is, search here in the forums or just Google it. In short, it is a way for javascript running in the browser to make separate HTTP/HTTPS requests to your web server. It can do this "in the background" (asynchronously) while the user continues to use the browser application.

So while not truly stateful, you can build a web app that appears to be stateful by having an AJAX request check frequently for server-side changes and then update the client browser accordingly. This could mean that the AJAX request actually retrieves the new information and dynamically changes the page contents or it could mean you fire a page refresh.

Depending on the number of simultaneous users you need to support and the power of your server hardware and bandwith, you can set your AJAX request to fire every 5 seconds or whatever makes sense for your app. You may be able to get away with 3 seconds, but 1 or 2 is simply asking a lot of a browser/webserver relationship. If you code it correctly, though, it won't matter if the request takes 10 seconds to return--you'll only set the timer for the next request AFTER the last request returns.

You may think to yourself, "gee, checking every 5 seconds just to see IF something changed seems really inefficient". But consider that it's something browsers and webservers do well and it's less expensive and more scalable than having every browser hold an open socket continuously.

I gave the example of a chat application. That is probably a good model for you to look at if you need an example. A search for +AJAX +chat +browser should give you some examples.
http://www.google.com/search?q=%2BAJAX+%2Bchat+%2Bbrowser&hl=en&sourceid=gd&rls=IBMA,IBMA:2006-33,IBMA:en&aq=t

commented: nice explanation.. Cheers ! +1

Thanks. I think I can get on with it now.

You could use the jQuery Javascript library that allows you to create simple Ajax interactions.

You can download a plugin that will send AJAX requests per second, 2 second, whatever you want.

You can get it here:

http://www.ajtrichards.co.uk/heartbeat

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.