3

I have to build an online application that shows a table with content being synchronized. That means, if the content of a table cell is being changed, every user has to receive the new content without reloading the site. A time delay of a couple of seconds means no problem.

The site is running as PHP content management system. There will be not more than 10 users at same time.

How would you do that? Using JS server-sent events? Interval of AJAX requests?

Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27
Alex
  • 101
  • 4

3 Answers3

0

This is what websockets are all about - but if you need to support older browsers then have a look at comet.

symcbean
  • 47,736
  • 6
  • 59
  • 94
0

On your server, you would have some code to build the table and deliver it as HTML.

Your webpage has a div element with dynamical content. You could do an AJAX request every 1 second to call the above mentioned PHP script, take its output and fill the div with its content.

  • "*You could do an AJAX request every 1 second to call the above mentioned PHP script, take its output and fill the div with its content.*" then how would a user change the content if you only poll but never push to the server? – VLAZ Mar 05 '19 at 14:35
  • You can attach AJAX requests to user actions as well, for example button clicks (or even just typing into a field). Those POST requests would be handled by the server and change something in a database or file. To build the table, the server would look at the database / file contents every time. – William Randokun Mar 05 '19 at 14:36
  • Though I just realised, if the whole div gets updated every second, the user has almost no chance to make an update. – William Randokun Mar 05 '19 at 14:39
  • Yep, that's the next problem you face. And after that is resolved, comes the merging of two users' changes that happened simultaneously (or close enough together). This is tied to locking data for a changeset, too. The whole thing is not simple and a bit broad for a question. – VLAZ Mar 05 '19 at 14:44
0

You could do it with AJAX polling, with refreshing the page using a meta tag inside your HTML document with <meta http-equiv="refresh" content="5"> (content is the time in seconds) or with something more sophisticated like Socket.IO.

pmatsumura
  • 391
  • 1
  • 6