When several users are viewing a record, and 1 person is updating the record, how to notify another record, is it updated?

In my Project Management application, I want to solve a problem when several users are viewing the same task record, and 1 or more of them are updating some of the Tasks data, while others are viewing it.


Here is the script ...

User # 1 views Task No. 1 and User # 2 views Task No. 1

User # 2 is now updating Task No. 1 Description

User # 1 is now viewing the task entry, but his view shows another obsolete Description , since only User # 2 is! Worse, he may want to edit the description on his own, which would completely rewrite the User # 2 Description update.


This is one example of a problem. Just add more users and the problem multiplies!

Ideally, I would like to use something like Sockets or the Pusher.com service to update data for all users as soon as any user updates it. However, this project will run on hundreds of servers and has limited capabilities regarding server requirements, so the question of sockets and even a service like Pusher is out of the question!

Another solution idea is based on what Twitter does. If you are viewing a Twitter page for people and they are creating a new post while you have a page loaded there. It will show a message with a DIV notification to inform you that there are X new messages and you will receive a link to restart the message flow with the latest messages.

enter image description here

I believe that a similar approach could work for my project. If the user does an update for any task data, and other users view this task record. It will display the โ€œNotificationโ€ message in the โ€œTaskโ€ modality window, informing the user that the โ€œTaskโ€ data has been updated and that they should restart the task.

To do this work, I know that there must be some AJAX request made at some interval.

Then, for an AJAX request, you need to compare the timestamp of the last update made in the Task entry and compare it with the time when the user looked at the Task entry, starting to view it or since the last time they restarted the task.

I feel my logic is missing a piece of the puzzle? Is this all right or am I missing something?

Can someone explain how I can do this or say, do I mean correctly?

I know that, in short, I just need to determine if the last modified timestamp of the task was AFTER the other user started viewing the Task. At some point, although I feel that the time for users should also be updated?

UPDATE

I completely forgot that Qaru performs this exact task on questions and answers! When you browse the page on SO and the response is updated, it will display a message notifying you to reload the response and provide a link to reload it. Here is what I want to do!

StackOverflow uses Web sockets for this, but in my application, which is a plugin for various server configurations, I cannot use Sockets. I am trying to achieve a similar result with AJAX. Even if he requested AJAX every 30 seconds to get a given time change and compare it with another, to determine if the user should reload the task data, will work in my case

enter image description here

+8
javascript php mysql
source share
2 answers

Your question is too broad, but you are mostly describing Pub / Sub.

Idea

Whenever a user enters your site, he receives a token to identify them.

If he turns to a task, he subscribes to this task, which means that any modification on it will be warned about it.

He polled the server to check if it has any warning.

Implementation

As for the implementation, you can set up a list with each subscription per user. Using your example:

User 1 subscribes to (Task1, Task2)

User2 subscribes to (Task1)

With each subscription, you save a value representing the last state that the user has regarding this topic (for example, the timestamp of the last modification).

A user checks your application every n seconds. Whenever the request reaches your application, you check the subscription for the user and check if the timestamp has changed (if they have the latter). If so, you update the last state that the user has and receives new values โ€‹โ€‹from him for mutable tasks.

What to consider

This subscription list will be constantly available, so be careful when you plan to keep it. If in memory, think that you will need to share it through different instances (if you are balancing the load). You can use Redis or something like that.

You do not need to access your database every time you need to receive data. If someone subscribes to it, store it in the cache.

+1
source share

The concept and idea are quite trivial, and the implementation should not be more complicated. You need the Last updated time stamp for each task, as you say, as well as the Last update mark on the client if you go to the described approach. Typically, when a user views a task , you should (on the client side, Javascript):

  • Request a server to mark the Last update of the Task viewed with AJAX.
  • If the time stamp of the Last update of the task being viewed is larger (newer) than the time stamp of the Last update in step 5 of the client transition.
  • Wait (asynchronously) n seconds.
  • Go to step 1.
  • Inform the User that the viewed Task has been updated.
  • The end (since the client already knows that the Task has been updated, there is no need to continue the survey whether it was updated or not).

One approach could simply be to create an asynchronous interval using setInterval () , which will be cleared after it is determined that an update has occurred, and then a message will be displayed to the user.

var lastUpdate = Date.now(); var intervalDuration = 30000; // 30 seconds var interval = setInterval(function () { var xhr = new XMLHttpRequest(); ... xhr.onload = function () { if (...) { // if the Task Last Updated timestamp is newer than lastUpdate clearInterval(interval); // show message to user that the Task has been updated } }; }, intervalDuration); 

I can imagine that in a significantly massive system this approach can be easily inadequate, but for solutions that are not related to corporate standards, or for subsequent improvements to the user interface, this can be a quick, cheap and impressive solution.

Of course, there are potentially more reliable and flexible alternatives to this: a long survey or websites .

+1
source share

All Articles