title: Monitoring User Sessions to Prevent Editing Conflict
body: <p>I'm working on something similar to a pastebin (yeah, it's <em>that</em> generic) but allowing for multiple user editing. The obvious problem is that of multiple users attempting to edit the same file. I'm thinking along the lines of locking down the file when one user is working on it (it's not the best solution, but I don't need anything too complex), but to prevent/warn the user I'd obviously need a system for monitoring each user's edit sessions. Working with database and ajax, I'm thinking of two solutions.</p>
  
  <p>The first would be to have the edit page ping the server at a arbitrary interval, say a minute, and it would update the edit session entry in the db. Then the next time a script request to edit, it checks for the most recent ping, and if the most recent was another arbitrary time ago, say five minute, then we assume that the previous user had quited and the file can be edited again. Of course, the problem with this method is that the assumption that the previous user had quited is simply an assumption. He could be having flaky wi-fi connection and simply dropped out for ten minutes, all the time with the window still open.</p>
  
  <p>Of course, to deal with <em>this</em> problem, we'd have to have the server respond to new request from previously closed sessions with an error, telling the client side to point out to the user that his session has ended, and then deal with it by, say, saving it as another file on the server and asking the user to manually merge it, etc. It goes without saying that this is rather horrible for the end user. </p>
  
  <p>So I've came around to think of another solution. It may also be possible to get a <code>unload</code> event to fire when the user's session ends, but I cannot be sure whether this will work reliably. </p>
  
  <p>Does anybody has any other, more elegant solution to this problem?</p>