Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So I'm working on a discussion platform, and various clients will visit http://host/thread.php, which will render the discussion thread to date in addition to a form to submit a new post. When a new post is submitted, I would like all of the other clients with browser windows open to have it appear in near-real-time.

One of the constraints of my script is that it may not use a DBMS and it must stay in the filesystem. Additionally, I can't use any PECL/PEAR extensions like inotify or anything like that for IPC.

The flow will look like this:

  • Client A requests thread.php and the thread is so far empty, but nonetheless it opens a Server-Side Event at eventPusher.php.
  • Client B does the same.
  • Client A fills out a post in the form and and submits (POSTs) it to subHandler.php.
  • ??? (subHandler stores the new submission into the main thread storefile which gets read from when a fresh, new client requests thread.php, in addition to somehow signalling to the continually-running eventPusher event-source that a new comment was posted and that it should echo the event-json to the client. How, exactly, it will send this signal I'm yet unsure of, but there are a few options that I've thought of -- this is the crux of the question, so see below for more clarification)
  • eventPusher.php happily pushes the new event to the client and it shows up soon after it was originally submitted on all clients who have the page open's screens.

Now for the #4 missing-link mystery-step, I see a few problems. I mean, either way, eventPusher is gonna be doing a while loop of some sort -- it's gonna be polling something, I think that much is clear. (If that's a bad assumption please do let me know.) Now, the simplest way would be subHandler gets invoked on the form submission, writes it to the main store in addition to newComments.xml, then exits without doing anything else. Then eventPusher checks in newComments.xml every X seconds (by the way, what would be a reasonable time interval here?) and if it finds something then it emits an event to the client. Now, my fear with this is that the server's hard drive will have to constantly start spinning up. Maybe this isn't the case, perhaps it would just get cached in RAM and the linux kernel would take care of this transparently such that filesystem access doesn't actually engage the device because the kernel knows that that particular file hasn't changed since last read. * idea #2: I have no idea how to go about this, but perhaps there is a variable scope that gets stored in general RAM on the system which can be read by any process. Like if we mega-exported a bash variable so that $new_post is normally false but it gets toggled to true by subHandler, and then back to flase once it's pushed to the client. I doubt there's such a variable scope in PHP directly, but I struggle with the concept of variable scope, I just can't seem to understand it no matter what I read on it. * idea #3: eventPusher queries ps in its whileloop for another instance of itself. If there's not another eventPusher active then it's highly unlikely that new comments will be getting submitted. It's okay if this only works >=90% of the time, it doesn't need to be completely foolproof. * idea #4: eventPusher queries DMESG to see if that file's been written to recently.

So to sum everything up, I need to have inter-php-script-communication in near-real-time that will work on a standard mod_php shared hosting setup without any elevated privileges, PHP addon modules, or other system adjustments that can't be done from the PHP script itself at runtime. With*out* spinning up the drive more than a few times. No SQL servers either.

Apologies if my english isn't the best, I'm still trying to improve on it.

share|improve this question
    
BTW, I just found this. Do you think it applies here? I'm just afraid that if its a different process reading and writing to the file that it won't be recognized as the file that's cached. Even if that answer does have me covered, I'd still appreciate any input and suggestions on my implementation, I could use some mentoring/guidance on this one. :) –  jackie Jul 4 '12 at 7:22
    
It does. Also, you should not be polling really - it will just burn CPU cycles. If you want to check for directory changes, there is inotify which even has an interface in PHP. Other than that, it is a development question, try StackOverflow for details. You also should not be concerned about "drives spinning up" - server drives are spun up - continuously. –  the-wabbit Jul 4 '12 at 7:48
    
Those (rather artificial) constraints make this reek of homework. –  womble Jul 4 '12 at 7:49
    
I doubt you'll be able to achieve this with your constraints. –  gekkz Jul 4 '12 at 8:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.