Search code examples
google-apps-scriptpolling

How to dynamically update a page served by a Web App?


I'm developing a Web App using google apps script and a spreadsheet as storage.

Basically, an HTML showing some tables for the different tabs.

From my app, users can add new tasks, edit tasks, and mark tasks as completed. Since there are many users using the app, the data showed on each client will get outdated very fast.

I would like to update the app with new records and changes to the existing ones as soon as possible.

I thought in logging the last edit tab+row in a cell and pull that data from the server every minute, but, what if many entries are added/edited during that minute?

I think WebSocket is not possible. Any other idea?

I'm using JQuery client-side.


Solution

  • To help avoid conflicts, give every task a unique ID. Something like creation time + random string. That way you can look it up in the spreadsheet. Also, I think the Lock Service can prevent concurrent edits temporarily to avoid conflicts: https://developers.google.com/apps-script/reference/lock/

    To check for updates, try polling the last edit time of the spreadsheet. If it's greater than the previous poll, fetch updates. https://developers.google.com/apps-script/reference/drive/file#getLastUpdated()