1

I need to know how I can refresh a div in real time for all users at the same time?

I have searched and searched for days and still haven't found a solid answer to this other than AJAX but when I try to find an example (even a simple example would do) so I can understand it, all i found was using AJAX to refresh a Div for one user (client side)!

is this a top coding secret that is kept from newbies like me or noone really knows the answer?

I even found the same question was asked on stackoverflow but again all the answers were "AJAX". okay lets say we are going to use ajax, but all the examples, tutorials are about refreshing a div for one user Not all the users!

Could someone point me in a right direction please? any examples, tutorials would be appreciated.

EDIT:

One example of what i mean is the timers on penny auctions! the timers and the highest bidder will be shown at the same time to all the users! there is no delay and even if there is its only a few milli seconds.

  • 3
    Search for ajax polling and socket programming – M Khalid Junaid Sep 20 '13 at 09:55
  • 1
    If you need "for all users at the same time" then an asynch technology like http isn't a good option... but why is it so business critical that this refresh must be absolutely synchronised – Mark Baker Sep 20 '13 at 09:56
  • Websockets are probably your best bet. – Ben Fortune Sep 20 '13 at 09:57
  • Do you need a bi-directional, full-duplex transmission between server/client (channel/subscriber)? - Node.js does it, also there is twisted available for python, and event-machine for Ruby, by which you can achieve same. Look for something similar solution for PHP. – Vishal Sep 20 '13 at 10:02

4 Answers4

1

You can refresh your data with setting a refresh time. Like described here: http://matthewaprice.com/simple-jquery-ajax-auto-refreshing-div/

Hope this will answer your question :)

*Edit; never mind just read you also read about refreshing option. My bad, did overread that.

Joran Den Houting
  • 3,149
  • 3
  • 21
  • 51
0

I would suggest starting with a single user Ajax script and then expanding on that. You could create an ajax script that on the client side asks the server "Anything to tell the users?" Every 5 seconds or so. The server could then check a database if there is something to inform the users.

Like this, all users are polling every 5 seconds for updates and when you do have something to say, it will be displayed with an 5 second margin for all users if the polling works. The server controls the time, the client just polls.

After getting the message to show to the users from the server, refreshing the div should be trivial, something like $('#TheDiv').html("<p>" + yourservermessage + "</p>");.


Edit after comments: if performance or other issues demands it, using a synced option like web sockets might be prefereable. For those, check out http://socket.io/ and this related SO question. It does get a little more complicated compared to traditional polling though.

Community
  • 1
  • 1
Joel Peltonen
  • 13,025
  • 6
  • 64
  • 100
  • Wouldn't this burden the server? 5 secs per user? – asprin Sep 20 '13 at 09:58
  • not really, well depends :) if server gets to return just FALSE (do nothing) its OK I think. But this wont be synced as expected, there is going to be user that can load new div 4 seconds later. – Kyslik Sep 20 '13 at 09:59
  • 1
    Yep! Synchronization is the keyword here. That is what led me to think it would burden the server – asprin Sep 20 '13 at 10:01
  • Actually, web sockets or some other synced option most likely would be more performant. The situation would have to be benchmarked. I would do this because it's very simple to build something like this - if/when the need for something more performant would arise, I would just replace this system with sockets then. – Joel Peltonen Sep 20 '13 at 10:04
  • @asprin, you are right. Synchronization ( showing the result at the same time to all users) is what I am after. – EnergyLynx EnergyLynx Sep 20 '13 at 10:09
0

I maybe old school here but an iframe with a meta refresh wouldn't do the job ?

Amine Matmati
  • 283
  • 1
  • 9
0

Use Websockets, there are good librarys for that already online

http://socketo.me/api/namespace-Ratchet.html

or

http://socket.io/

joschua011
  • 4,157
  • 4
  • 20
  • 25