Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Php programming problem?

  1. Apr 29, 2007 #1
    How can i use php to write a real-time chatting script like msn or yahoo messenger ? I know i can use php and mysql and check for new messagesin mysql table every seconds but this will cause high traffic . So is there any other way ?
  2. jcsd
  3. Apr 29, 2007 #2


    User Avatar
    Science Advisor

    You can use a reverse Ajax approach to reduce traffic. Or you can use any platform that supports sockets (i.e. flash or java).

    With reverse Ajax, instead of querying the server every X seconds for new messages, you query the server once. The server holds the request (without timing out) until it has a message to send back to the client.
    The client then receives the message and immediatly initiates another query to the server which holds the request again until it has something to return, thus repeating the process. It's important that the server be able to hold requests without wasting resources (such as hold the request with an active loop), otherwise you'll have high CPU usage at the server side.

    With Flash you can use the XMLSocket object to connect to a port at the server. With sockets the server can send messages directly to the client, without the client having to monitor the server for new messages (i.e. once there are new messages they'll arrive at the client's end of the socket), so the process becomes more efficient.
    You'd either use an existing server, such as the Flash Communication server (which i think now is called the Adobe Media server or something), or use PHP scripts as the server, or build your own server software which doesn't have to be a complex task.

    Java would be similar to Flash.
    Last edited: Apr 29, 2007
  4. Apr 29, 2007 #3
    Can you give me more details about the reverse ajax ?About the flash , can i use the normal server or i need to use the Flash Communication server .PHP also support socket right?
  5. Apr 29, 2007 #4


    User Avatar
    Science Advisor

    You don't need to use the Flash Communication Server with flash. You can build a server with Java, .NET or PHP. If you use PHP you will need to run the PHP app independently from the web server (i.e. not on top of an IIS or Apache web server), using the PHP CLI for example, as described here:

    Once you have a server running, you create a flash movie that instantiates an XMLSocket and connects to the PHP server, and exchanges any necessary information back and forth.

    I actually recommend the Reverse Ajax approach because it's a fairly new approach, more compatible (no flash/java plugins) and more accessible and easier to use.

    You'll still use PHP and MySQL for storing and accessing user messages. The difference is in how you use Ajax. Commonly you'd use Ajax to query a PHP script (which checks for new messages on the database) every x seconds, which generates alot of traffic.
    What you need to do is find a way to hold a request at the server until you have something to return.

    Then for example at client side you use Ajax to query the PHP script (same as before). The PHP script checks for new messages. If there are any to return then it returns them, otherwise it waits (sleeps or blocks with a loop) checking for new messages every so often (so now the PHP script checks every x seconds, rather than Ajax). Once there are new messages to return then the PHP script stops sleeping or blocking and finally returns a response containing the messages.

    At the server side there's usually a timeout limit. For example if a request takes more than S seconds to process or more than a given number of resources (CPU time, Memory, etc) then the server (IIS, Apache...) terminates the process and returns a time out back to the client.
    You can either avoid timeouts or just deal with them.
  6. Apr 30, 2007 #5
    I don't know how to use reverse ajax and also how to avoid timeout ,please give me some example.
  7. Apr 30, 2007 #6


    User Avatar
    Science Advisor

    Well, i'm actually involved in a project that will use reverse Ajax, but it's in ASP.NET. Maybe you can take something from this. If you've ever used threads then you have come across the Sleep() function. Basically when you make an HTTP request to a web application running on a web server (IIS, Apache...) the web server's worker process (w3wp.exe in IIS) responsible for the application either starts a new thread which will service the request or queues the request if all allocated threads are already being used.

    This is true for PHP web applications as well. When you make a request to a PHP web application, the web server either starts a new thread to service the request, or queues it until a thread is available (the maximum number of threads is customizable).

    Our goal is to block the server's response to the Ajax request until the server has something to return. When the Ajax request reaches the server it will eventually be serviced by a thread. If you want to delay the processing of a thread by 5 seconds you can use Sleep(5000) in ASP.NET/Java or Sleep(5) in PHP. Therefore the following code (ASP.NET) will delay the server's response until it has something to return:
    Code (Text):

    //we get here if there's something to return so write that something to the client
    You should be aware however of some implications of using this strategy. Because most servers have a specifiable maximum number of threads, each reverse Ajax request that blocks for say 40 seconds is completely blocking a whole thread. If you have a max of 25 threads, then 25 reverse Ajax requests would completely slow down your server.
    So you either need to ensure that you have sufficiently high max number of threads such that you can be confident that you server will always offer good performance, or just not use Web Server worker threads for processing these types of requests, which i recommend (though it involves a little more work).
    In ASP.NET this is made easier with Asynchronous handlers, which will run on a separate thread that you create and hence doesn't take over one of our limited web server threads.
    Learn more about it here: http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/
    An alternative to ASP.NET would be doing a server in Java or .NET or PHP. PHP probably has something equivalent to Asynchrnous handlers but it's not something i have looked into.

    Anyway, if you're not doing a web app that will be used by hundreds of users concurrently then you can use PHP with Sleep (making sure to specify a large thread maximum).
    Last edited: Apr 30, 2007
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Php programming problem?
  1. Programming problem (Replies: 8)