Asked  7 Months ago    Answers:  5   Viewed   35 times

I was thinking of implementing real time chat using a PHP backend, but I ran across this comment on a site discussing comet:

My understanding is that PHP is a terrible language for Comet, because Comet requires you to keep a persistent connection open to each browser client. Using mod_php this means tying up an Apache child full-time for each client which doesn’t scale at all. The people I know doing Comet stuff are mostly using Twisted Python which is designed to handle hundreds or thousands of simultaneous connections.

Is this true? Or is it something that can be configured around?

 Answers

48

Agreeing/expanding what has already been said, I don't think FastCGI will solve the problem.

Apache

Each request into Apache will use one worker thread until the request completes, which may be a long time for COMET requests.

This article on Ajaxian mentions using COMET on Apache, and that it is difficult. The problem isn't specific to PHP, and applies to any back-end CGI module you may want to use on Apache.

The suggested solution was to use the 'event' MPM module which changes the way requests are dispatched to worker threads.

This MPM tries to fix the 'keep alive problem' in HTTP. After a client completes the first request, the client can keep the connection open, and send further requests using the same socket. This can save signifigant overhead in creating TCP connections. However, Apache traditionally keeps an entire child process/thread waiting for data from the client, which brings its own disadvantages. To solve this problem, this MPM uses a dedicated thread to handle both the Listening sockets, and all sockets that are in a Keep Alive state.

Unfortunately, that doesn't work either, because it will only 'snooze' after a request is complete, waiting for a new request from the client.

PHP

Now, considering the other side of the problem, even if you resolve the issue with holding up one thread per comet request, you will still need one PHP thread per request - this is why FastCGI won't help.

You need something like Continuations which allow the comet requests to be resumed when the event they are triggered by is observed. AFAIK, this isn't something that's possible in PHP. I've only seen it in Java - see the Apache Tomcat server.

Edit:

There's an article here about using a load balancer (HAProxy) to allow you to run both an apache server and a comet-enabled server (e.g. jetty, tomcat for Java) on port 80 of the same server.

Wednesday, March 31, 2021
 
Nil
answered 7 Months ago
Nil
98

if there are no messages to push from server in say 40 sec, you send some response from server, on the basic of which the client re-request.

Wednesday, March 31, 2021
 
AntoineB
answered 7 Months ago
62

In PHP, regexes have to be enclosed in delimiters, like /abc/ or ~abc~. Almost any ASCII punctuation character will do; it just has to be the same character at both ends in most cases. The exception is when you use "bracketing" characters like () and <>; then they have to be correctly paired.

With your original regexes, the square brackets were being used as regex delimiters. After you glued them together it no longer worked because the compiler was still trying to use the first ] as the closing delimiter.

Another problem is that you're trying to use square brackets for grouping, which is wrong; you use parentheses for that. If you look below you'll see that I replaced square brackets with parentheses where needed, but the outermost pair I simple dropped; grouping isn't needed at that level. Then I added ~ to serve as the regex delimiter. I also added the i modifier and got rid of some clutter.

~$?{[w-]+(?::[a-z]*)*}~i

~<[a-z]+:[a-z]+s*(?:s[a-z]+="[a-zds]+"){0,5}s*/>~i

To combine the regexes, just remove the ending ~i from the first regex and the opening ~ from the second, and replace them with a pipe:

~$?{[w-]+(?::[a-z]*)*}|<[a-z]+:[a-z]+s*(?:s[a-z]+="[a-zds]+"){0,5}s*/>~i
Saturday, May 29, 2021
 
Norgul
answered 5 Months ago
42

It's likely because of output buffering. Try adding this at the top of the file to close all the open buffers:

while(ob_get_level() > 0) {
    ob_end_flush();
}

You can also add ob_flush() after the flush() command in your code:

$i++;
echo $i;
flush();
ob_flush();

(Note that you should only have to do one of them, not both, but try it)...

Saturday, May 29, 2021
 
matthy
answered 5 Months ago
87

You may want to look at WebChat2 This project uses comet, AJAX, and a custom HTTP server to communicate with IRC via sockets.

Wednesday, July 28, 2021
 
tika
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :