Re: Chat with php

From: Date: Mon, 07 Oct 2002 22:04:15 +0000
Subject: Re: Chat with php
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-119147@lists.php.net to get a copy of this message
Hi, i'm running a selfmade PHP-chat since one and a half year, currently version 3. Maybe we can share some ideas.... The image-idea is interesting, but would take heavy bandwith i think. My chat runs over a simple dsl-line with 256kbit upstream, there are up to 50 users at the same time (slow but okay). It's very good at 30 users, but there is a whole community with galleries etc. on the same server. What i know: To avoid reloads, you must use a "stream". That's the flush()'n'sleep()-thing. This is my main-loop: 01 ini_set("max_execution_time", "0"); 02 for(;;) 03 { 04 $newline = get_newline($last_row); // fetch a new line (out of an database for example, returns time, alias, text as array, or false if no new line is found) 05 if(is_array($newline)) 06 { 07 $newtext = format($newline); // format line for output (text "bla" could be "(01:01) Alias: bla" and "/me bla" could be "Alias bla", returns the string to be outputtet to the browser) 08 if($newtext AND $newtext != $oldtext) echo $newtext; // if it's new text (avoids flooding), output the line! 09 $last_row = $newline["id"]; // next time fetch another id 10 flush(); // sends buffer to browser 11 echo "\n<!-- for Opera -->\n"; // Opera seems to use a strange buffer and does not display the last line, so send another one, invincible 12 flush(); // flush again, opera should now display the line 13 } 14 unset($newline); 15 16 usleep(200000); // sleep some time, otherwise the script would eat 100% CPU. Usuable on my server (p3-550) are 100000 to 500000 msec 17 18 $loop++; 19 if($loop > 600) 20 { 21 echo "<!-- TOK -->\n"; // some browsers kill the connection if it is dead for some time, so send a "time out kill", some minutes are enaugh 22 $loop = 0; // reset 23 } 24 } Okay, tha's the deal...... max_execution_time is set to unlimited, I had streams running several days..... Fortunately PHP kills the scripts automatically if no brower is listening. To fetch a line there are some ways.... the most simple is to query a database for a line with an higher id, but this eats up loads of cpu-usage in bigger chats. Currently i use shared memory. I've an php-console-script running wich fetches newline out of the database and writes 'em into shared memory. This is MUCH faster than have every stream-script connecting to the db. What i don't know: I'm searching for an better idea to interchange data between the scripts wich enter data, the server and the stream-scripts. Shared memory is good enaugh for the last two one, but it ever takes min. the 200000 ms sleep-time for a new line. I need something to awaik the script exactly when a new line is in memory. Synchronisation between enter and server is more difficult, because there is the posibillity for two scripts entering a new line at the same time.... don't know how to avoid this. I hope this helps. Cya, Thomas 'Neo' Weber --- thomas@youngarts.org neo@gothic-chat.de

« previous php.general (#119147) next »