2

I have this situation: 100 computers are displaying a webpage and from a backend I can update this webpage.

And I would like that the webpage displayed on my 100 computers is automatically updated.

Is there a way to refresh/update the page without polling? The best way it will be that an external page can force the page to reload, but I think that's not possibile.

Thanks

M.

user3129728
  • 23
  • 1
  • 4

4 Answers4

1

You could define a route to your server that return true or false depending on the need to refresh or not. You would then do on client side :

function checkReload() {
        $.ajax({
           url: "127.0.0.1/needRefresh",
           success: function(data) {
              if (data == "true")
                 location.reload();
              else
                 setTimeout(checkReload, 1000);
           }
        });
}
setTimeout(checkReload, 1000);

What this code do is every second (setTimeout function), it query the server with ajax to know if page needs to be reloaded, then reload or not depending on server's response.

Yabada
  • 1,728
  • 1
  • 15
  • 36
  • never user set interval...and that too with an interval of one second..you will kill the browser.Use set timeout instead – HIRA THAKUR Dec 23 '13 at 13:59
  • Why never use setInterval ? setTimeout isn't the good choice here expect if you setTimeout inside setTimeout...witch is basicaly setInterval... Also, the one second timeout is for example only. – Yabada Dec 23 '13 at 14:01
  • no you are getting it completely wrong..You should understand the basic nature of setTimeout and interval...setInterval queues up the output irrespective of the first request is completely over..setTimeout is a nice tool in this perspective..just call the same function at the end which will act as an callback. – HIRA THAKUR Dec 23 '13 at 14:04
  • Edited my answer to match your advice – Yabada Dec 23 '13 at 14:08
1

You can use WebSockets. For example:

var socket = new WebSocket("ws://javascript.ru/ws");
socket.onmessage = function(event) {
    if (event.data == "reload") {
        location.reload();
    }
};

With WebSockets you can also organize reloading all pages from one of this pages with propagation of event.

WebSockets implement a event-driven architecture. You will implement some handlers for events and messages and just start it. Then you can send events from server or other pages through server and handle it as you like.

1

On recent browsers, there is an implementation of websockets that allow the server to push data to the clients. It only works on some webservers though (tomcat 7, node.js, etc...) Websocket spec is here.

An other method is the long request in which the client starts an http request, and the server only answers when it has something to send to the client.

More info here.

Community
  • 1
  • 1
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
-1

With Javascript : No.

With any server side language: yes.You always have the privilages of using a CRON or polling.

Refer:Is "long polling" the most efficient way to create a Web Real Time App?

Community
  • 1
  • 1
HIRA THAKUR
  • 17,189
  • 14
  • 56
  • 87