0

I'm running php/mysql and have a pretty complex html-table where every cell can be edited. Some of them just text while clicking on other cells brings up select menus, radio buttons etc.

When a user makes a change in the table, all the other users need to refresh their browser to see the change.

1) Is there an easy way to make my table "pushable" so new content shows up for other users when cells are edited?

or

2) Can I run a javascript every minute or so that will fetch the latest content in the background and compare it to the current page and if anything has changed perform an update?

Thank you for ideas

SeaBass
  • 1,584
  • 4
  • 20
  • 46
  • You could use `server sent events` - much easier to get running than `websockets` and would push new content, via an `Event Stream`, to the clients - the callback function would then re-render the html – Professor Abronsius Jun 13 '17 at 06:36
  • @RamRaider Thanks! I've googled and found some info about it, but if you know a good starter guide please let me know. – SeaBass Jun 13 '17 at 22:13
  • With regards to `SSE` an answer I provded some time ago shows the basics of the server side code - https://stackoverflow.com/questions/29480791/while-loops-for-server-sent-events-are-causing-page-to-freeze ~ should be moderately straightforward to implement your code within the loop. – Professor Abronsius Jun 14 '17 at 05:39

1 Answers1

0

The first method you described is called WebSockets. WebSocket allow bidirectional communication between the client and server. They communicate using messages and payloads are commonly passed as JSON. The client (where updates are made) would emit a message to the server saying the table has been updated along with the updates. The updates would then be broadcast to all other clients to where they'd then rerender the table.

The second method you described is called polling where an Ajax request would be made periodically inside a setInterval / setTimeout.

I would always try and use WebSockets nowadays. There'd be far less overhead compared to using a polling technique. However, as you're using PHP, I think the second option would be more favourable due to the locking nature of PHP sessions. Support for WebSockets isn't brilliant in PHP, you'd have to use a third party library such as Ratchet.

Thomas Maddocks
  • 1,355
  • 9
  • 24