How to make pages update immediately after a database update

The scenario is that say a user (user #1) updates some info via some form and the code resets the dataset. Meanwhile, another user (user #2) is viewing that same page while the user #1 is updating the data. When user #1 is done updating and it is updates, user #2 does not see the updated data rather the old data. In this case, how would user #2 be able to view new data without reloading the site?

Iā€™m not sure whether this is really possible as this would need to push some data or trigger to the user #2ā€¦

Have you read about Hooks?

They are used to execute code in the backend of your collection to make validations etcā€¦ but maybe you can get it to work as a push notifier for your user #2? Just try and playā€¦

But what could maybe work isā€¦ to set a flag (additional column) ā€˜in editā€™ in your collection for this element user #1 is viewing by using hooks or by setting it by pressing a button ā€˜Editā€™ā€¦ this flag you could use to show user #2 that someone edits this element of your collectionā€¦ disabling the ā€˜Editā€™ buttonā€¦ you could either query the entry again every 5sec using Timeouts to load the new results or swap the ā€˜Editā€™ button by a ā€˜Refreshā€™ button?

Just thoughtsā€¦ just try and play :wink:

Thanks! Iā€™ll try that. Ingenius thinking.

Balaram, just two observations if you are going to implement the solution presented by Tom:

  1. if you refresh (by pushing a button or automatically) the form on-screen, user2 will loose all his edits he just typed in. Most users are not too happy with that
  2. setting flags will, at some point, fail. Suppose that user1 retrieves a row for update and you set a flag ā€œIneditā€ or something, Now user1Ā“s pc crashes or power goes down. He reboots, will try to retrieve the same row where he left off, but he now gets a message that someone else is editing it. But thatĀ“s not the case. It was the flag he set himself before reboot. Thus, the row stays locked forever, until an Admin resets the flag.

So, as a synthesis, yes, you could try to set a flag, but simply prevent two sided editing by showing an ISLocked message for user2 when he retrieves the same row, with a retry-button.
The perpetual lock problem will remain though, without native row-locking support from the database.

  1. setting flags will, at some point, fail. Suppose that user1 retrieves a row for update and you set a flag ā€œIneditā€ or something, Now user1Ā“s pc crashes or power goes down. He reboots, will try to retrieve the same row where he left off, but he now gets a message that someone else is editing it. But thatĀ“s not the case. It was the flag he set himself before reboot. Thus, the row stays locked forever, until an Admin resets the flag.

You could solve this with some additional codeā€¦ You could set a timestamp for each edit and do validations for each row with flag ā€˜ineditā€™ from time to timeā€¦ checking the age of each edit and resetting it to Null when the edit time stamp is too oldā€¦ just an ideaā€¦ there are maybe much more optionsā€¦

Yes Tom, you are right. And there are a couple of other solutions too (like on user). Thing is, these cleanup routines are already available in every good relational database that offers locking, automaticly. I myself do not need row-locking at the moment, but other users might, itĀ“s one of the first things you run into when using a database for adding and editing.
Maybe we could suggest this in a feature request?

1 Like

Could you write some javascript that runs in an iFrame and talks to the database via a wix-http-functions API? (essentially expose a REST API to the database). If the iFrame detects an update by polling the API it can post a message to the main page being edited and an onMessage handler could light up a badge showing that the data has been changed remotely?