Reputation: 115
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.
Upvotes: 1
Views: 2274
Reputation: 4207
Use Websockets, there are good librarys for that already online
http://socketo.me/api/namespace-Ratchet.html
or
Upvotes: 0
Reputation: 283
I maybe old school here but an iframe with a meta refresh wouldn't do the job ?
Upvotes: 0
Reputation: 13432
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.
Upvotes: 0
Reputation: 3163
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.
Upvotes: 1