0

When a user clicks the save button, I want all users currently in the page to be notified, or even better (or worse in a second thought) to enforce the page to refresh.

So, I tried this in my script:

function save_match() {
    ...
    alert("Saved slot " + slot + "! :)");
    window.location.reload();
}

but the refresh, of course, happens only for the user that actually pressed the button, the other users have no idea about the save the other user just made.

Enforcing the reload of the page seems a bit unlikely to happen, so what about doing something via JavaScript that would notify the other online users for the save incident?

Possible related question.


So, what I want is this:

function save_match() {
    ...
    alert("Saved slot " + slot + "! :)");
    // do something here so that all online users are updated!!
}
Community
  • 1
  • 1
gsamaras
  • 69,751
  • 39
  • 173
  • 279
  • I want the notification to happen when the save occurs, so I would need something in the end of the `save_match()`. @PaulG – gsamaras Jan 13 '15 at 21:27
  • Javascript can't do this by itself. You need to create a continuous connection between all online users to the server. – Starmetal Jan 13 '15 at 21:29
  • Any example @hardcoresquirrel? – gsamaras Jan 13 '15 at 21:31
  • I'll explain. You can do it with AJAX. That `save_match()` will create a request with a variable `refresh = true` or something like that. On server, it will set a global variable `everyone_refresh = true` if `refresh = true` is sent from client. Than, apart from this, we have another AJAX request every 1 second that will ask the server for `everyone_refresh` variable. If it returns true, `window.location.reload()`. Quite simple. Every client will know when to refresh. Of course, there are more thing to consider, like avoid overlapings etc. – Starmetal Jan 13 '15 at 21:41
  • It depends on platform, but this is the doggy way, fast, simple but not so elegant. There are websockets now, to create a continuous connection between client and server. – Starmetal Jan 13 '15 at 21:43
  • @hardcoresquirrel can you make a nice answer with that so I can understand it please? :) – gsamaras Jan 13 '15 at 22:07
  • there is nothing more to say, just put it in practice, 2 ajax request and 2 lines of server-side code... – Starmetal Jan 13 '15 at 22:10
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68774/discussion-between-g-samaras-and-hardcoresquirrel). – gsamaras Jan 13 '15 at 22:11

1 Answers1

2

You need to implement a Server Push. This technology has different working strategies like Long pooling, Heartbeat and WebSockets. Check them out.

Edit: It is indeed possible with any of the strategies mentioned.

For long pooling, you send a request to the server and wait indifinetly until the server 'pushes' back any updates if there are any. Check this post to see how to do it with jQuery.

Heartbeat is a different and more newtork-consuming tecnique in which you send a request every x seconds to see if there are any updates from the server and if so, you update your data. Check this example of how to implement it.

You can also see an example of how to implement a WebSockets javascript application in the MDN but consider the browser support for this feature before jumping on it.

Roberto Linares
  • 2,179
  • 2
  • 24
  • 33