0

I'm coding a translation system for multiple messages.

I've a page with a table listing all the messages that need to be translated. When a translator click on a message to translate it, I lock it cause no other translator can be assigned to it.

The problem arises when a translator enters and instead to write something, leaves the page in an unconventional way like back button or closing the browser.

If this happens I want to unlock the message to make it available again to other translators.

How can I reach this behavior?

I know the javascript onbeforeunload event but triggers also every time a user refresh the page and this isn't what I want.

ty in advance

EDIT: seems that implementing a js ajax call to notify every minute the server is the way to go. Now the question is how to handle the PHP server side?

Fed03
  • 575
  • 5
  • 16

2 Answers2

0

What's wrong with using onbeforunload and asking the person if they really want to leave the page with unsaved changes?

I'd use the mentioned approach while proving a manual save button. Also, a timeout on locks would help, so if the person has not edited the field for several minutes, it'd be unlocked and the person would be notified via a JS AJAX call.

EDIT: to implement the AJAX timer, I'd do the following:

  • save last access time to the translation item in a database or in a file on the PHP side
  • once every 30 seconds, do an AJAX call to a PHP script that will verify the last access time
  • depending on the result, return an "OK" or "TIMEOUT" message from the PHP script to the JavaScript part, which will then either do nothing (for OK) or deactivate the translation and inform the translation about the timeout
Community
  • 1
  • 1
Zathrus Writer
  • 4,311
  • 5
  • 27
  • 50
  • lol, thanks @DiegoCNascimento :) ... also, I started it as a comment but realized it's way too big for it :P – Zathrus Writer Sep 13 '13 at 08:58
  • Ty, if u look at the converstaion betwenn me and @Diego u'll find that I need to implement a timer in PHP but I think it's not possible – Fed03 Sep 13 '13 at 09:41
  • wow, that conversation looks way too complicated! I'm updating my answer with a way I would get around the time thingy... – Zathrus Writer Sep 13 '13 at 10:25
  • @ZathrusWriter if the user closes the page what it wants to know, the script will not be called and no "TIMEOUT" will occur. It should use some timer server-side. – Diego C Nascimento Sep 13 '13 at 10:33
  • when the user closes the browser, a timeout will occur once another user opens the same translation - basically checking the old timeout, unsetting the lock if a timeout should have occured and creating a new lock for the new user who's just accessing it – Zathrus Writer Sep 13 '13 at 13:14
  • @ZathrusWriter I get it. But it needs requests to set the time-out, its a design choice. I particularly prefer the other method, but it will work. – Diego C Nascimento Sep 16 '13 at 15:52
  • @DiegoCNascimento setting the timeout could be done using the same request as when the user clicks into that translation to start translating it, or am I wrong thinking this? – Zathrus Writer Sep 16 '13 at 16:30
  • @ZathrusWriter well it will not be setting a time-out but verifying the last one and changing/or not the lock. The question is that till a user tries to edit, the lock will not be correct, that can or not, be a problem. – Diego C Nascimento Sep 16 '13 at 16:34
  • @DiegoCNascimento I see what you mean now, thanks for the clarification - and good luck with a resolution :) – Zathrus Writer Sep 17 '13 at 09:20
  • @ZathrusWriter thanks. I think the "good luck with a resolution" is to the OP? :) – Diego C Nascimento Sep 17 '13 at 12:19
  • @DiegoCNascimento you're right, it was to OP - lost track who actually asked the question and who answered it :D – Zathrus Writer Sep 17 '13 at 12:39
0

You can use WebSockets, but in my opinion its a immature technology and still away from being firewall friendly.1 So, you can use HTTP polling. Use a JavaScript to make a HTTP request to the server from time to time, so it will show to the server the client is still in the page. The time will depend on connection, number of user's etc, but putting it before the keep-alive expires is a good idea as the TCP/IP connection is still open.

If the user leaves the page, the polling will not execute, and after not receiving the HTTP request for x seconds plus some time, the server can assume the user is not more on the page.


1 - Well its the firewall that is not friendly of WebSockets, this probably will change with time

Diego C Nascimento
  • 2,801
  • 1
  • 17
  • 23
  • This can be reached using an ajax call every 30 sec or so but when this stops how do I catch it server side? – Fed03 Sep 13 '13 at 09:15
  • @Fed03 well, first AJAX is just a term that defines some strategies, what I said **is AJAX** if you prefer, I just don't like the term. You need to say what server-side technology you are using so we can help. Anyway what you need is to implement a time-out that will be cleared on receiving the request, and if its not you will do some action, in this case unlocking the translator. Adapt this for your server-side. – Diego C Nascimento Sep 13 '13 at 09:18
  • I'm using PHP and you're right talking of ajax... what do u prefer to use? – Fed03 Sep 13 '13 at 09:21
  • Well I'm not a PHP user, so you can search how to implement a timer in PHP, or just ask if its not asked yet. – Diego C Nascimento Sep 13 '13 at 09:32
  • No, for HTTP polling :) – Fed03 Sep 13 '13 at 09:38
  • @Fed03 you have all you need in that terms, JavaScript and a WebServer. You could use some third-party developed "helper" library, but its just for easier or not your implementation. – Diego C Nascimento Sep 13 '13 at 09:41
  • Well you could try reading this http://php.net/manual/en/class.event.php and this http://www.php.net/manual/en/event.timer.php – Diego C Nascimento Sep 13 '13 at 10:06