0

I am writing a JavaScript for an in-browser IM client for the sake of practicing and learning JavaScript and AJAX.

I need to be able to check for a change in the file size of a text file that is being used as a temporary storage for 40-80 SQL entries that contain messages so that it can update the display.

At the moment I am using a setInterval function to periodically check for a change in file size using short PHP script, but this can cause issues, if the interval is to long, messages are delayed, if it is shorter, it means a lot of php scripts running very quickly, which takes up server resources.

What is the best way to do this if the main concern is to reduce server resource usage? (I am running my server off of a rather low tech PC I've scraped together(2gb ram, 2.8ghz AMD seperon processor))

Preferably, I would want to do this using an AJAX event triggered by someone sending a message, I.E. When user B triggers the event that edits the file by pressing enter, that triggers a function on user A's side that updates the HTML file

Any ideas? I am open to any solution to this particular problem. I gave specific examples of what I want to happen in the specific languages in order to give a better idea of what it is I am attempting to do.

If there is a way to do this that isn't JavaScript/PHP, I'd also be open to exploring that as an option.

Enigmadan
  • 3,398
  • 2
  • 23
  • 35
riatzukiza
  • 72
  • 7

2 Answers2

0

Doing this with PHP can be a bit cumbersome. You could try doing something like long polling where you keep the HTTP request open until the server has new data to send to the user. If messages are sent frequently, this might not be ideal. You might want to consider using event-driven web technologies like node.js with something like Socket.IO.

In any case, you'll likely want to maintain a connection with the server if you want to get the message in near real-time. There are ways to use WebSockets with PHP as well, but PHP isn't really the best for this because it's not designed to keep scripts running for long periods (also see What exactly entails setting up a PHP Websocket Server?).

Community
  • 1
  • 1
Kyle
  • 2,822
  • 2
  • 19
  • 24
  • Hmm... i figured that doing it with php would not work. Thank you, i will look into node.js and Socket.IO – riatzukiza Jul 21 '13 at 03:46
  • You could come up with a solution with PHP, but it's not easy or likely recommended with this type of application. – Kyle Jul 21 '13 at 03:48
0

Browsers & HTTP/ AJAX generally work by a "pull" model. The browser/ or AJAX sends the server a request, then the server answers a response.

There isn't generally much provision for the server to contact the browser, to "push" an event. This can however be simulated by a long-running request, to which the server writes data when the event/ or events occur.

For example, this could be a request that answers "empty" after a timeout of 10-30 seconds.. or the server returns & answers immediately, if there are event(s) in its queue.


With a Java server this is easy to do, and I've used this successfully for event notification in a major integration project a few years back.

However I'm not sure in PHP how much ability there is (probably very near zero) to maintain an overall server state, coordinate or communicate between threads/requests, or maintain event queues.

You could look into something like a Java webapp running on Tomcat. All you need is a basic web.xml and one Servlet class, and you can build just about anything from there.

Thomas W
  • 13,940
  • 4
  • 58
  • 76