Sup everyone
I'm developing an application that is basically a chat room and am trying to optimize the way to do this. Rather then refresh or period ajax requests as I would have done before, this application needs to be pretty scalable so i've been attempting long polling and comet implementations.

Sadly PHP seems to not be the favorite tool for the job on this case, i've seen many cases (such as facebook) use more lower level languages to develop socket driven browser push rather then mysql/php to longpoll the server.

I'm thinking I can get by for now (up to a few hundred users at one) with PHP/Mysql so it's still an option for now. But I was just wondering what you're expereinces have been. I think its abundantly clear for me that if I'm going to continue my web dev carreer I need to expand beyond PHP/Mysql and js. I've dabbled in java as well but am now toying with C++.

anyway, anyone here ever implement comet well and if so what technique (hardware/software/language/resources) you used to achieve this. I installed LAMP on a machine rather than my wamp machine but being a windows guy I have trouble wrapping my head around linux (specially when I installed ubuntu server and it only booted me to a command prompt. that was fun)

    Have you ever thought about skipping mysql altogether and using app caching such as apc or memcache? It should be very fast and you can expire the content automatically with TTL. I still can't wrap my head around how anyone escapes polling from a browser or any other client side plugin that requires http? I suppose you can build your own java applet. Can they do their own sockets?

      Oh, I also suggest you install a normal Ubuntu distro. Server is great when it's truly a production box and even better when it's a remote server (I cut my teeth on Debian boxen long ago from command line.) However, the normal distro has everything you need (or you can get it quickly) and a GUI is nice to have to do development work on 🙂

        No there will definately be polling since there isnt a way yet to actually push data to the browser, only mimic it. I see opera, IE8 (we'll see) and chrome (badly) have taken steps to create server side events trigger the browser but I'm sure even then its still creating requests at some level.

        Thanks Bretticus, I'll look into caching and seeing if I can share messages between sessions without using mysql but my problem it was really having the polling be universal an efficient. If anyone has any more ideas or suggestions I'd appreciate it.

          rulian;10900753 wrote:

          Thanks Bretticus, I'll look into caching and seeing if I can share messages between sessions without using mysql but my problem it was really having the polling be universal an efficient. If anyone has any more ideas or suggestions I'd appreciate it.

          The app cache should be very efficient for sharing data across sessions since it's bound to one machine (apc) or a cluster of machines (memcache) and saved in memory. Facebook uses memcache for example. I suggest just getting APC if this will be one machine. Pretty easy to install on Ubuntu server.

          sudo apt-get install php-apc
          sudo /etc/init.d/apache2 restart

          From there, it's pretty easy to store data. It can be almost any kind of data too I believe (I've been storing arrays for caching queries.)

          The only way I know of to do polling would be via a javascript timer. I have used PeriodicalUpdater from the prototype javascript api in the past. It seems to work well. Very easy to use. Dunno how well that would work for just grabbing the latest messages (instead of updating the whole enchilada) though.

          Good luck. Hopefully somebody else can give you advice here too.

            thank you bretticus you've been very helpful
            I did some research on apc and found out is' beend discontinued for wamp
            I'll do my best with ubuntu but I'm afraid my lack of linux experience will hinder my ability to build this app.

            What do you guys recommend for getting up to speed with linux quickly, for example (and this should make you linux folk chuckle) I booted ubuntu and had to install some stuff and my first question was "wtf is sudo"

              Ha, I'll keep the thread going for you! Hopefully, I'm helping instead of hindering (folks think you're well taken care of.) FYI: sudo is the command line equivalent of User Access Control in vista or any privilege escalation prompt (like in OS X)

              I still recommend you install the desktop version of ubuntu.

              This is a VERY interesting subject to me (COMET) and so I've been reading (wasting time that I should be working) nd it seems every COMET implementation in PHP is done using hidden iframes and long polling. But (as you have discovered) keeping a connection open indefinitely using mod_php in apache is considered less than efficient. That's why I just suggested a setInterval solution to look for changes. However, whieol i was looking, I came across this js COMET library that claims COMET with PHP.

              Anyone else care to comment????

                Oh, by the way. I liked this tutorial for starting out using Ubuntu. Here's another good one. I like them because the show the basics of Ubuntu usage (installing software, etc.) Since you can do alot of stuff on the Linux command line, here is a tutorial to get you up to speed with that. Keep in mind, Linux is Linux. And it's based on Unix and so is OS X. So you will find many similarities among all the *nix OS's. Finally, here's a video that goes over the LAMP install on Ubuntu

                  Figured I'd move this to the "General" forum since it does have something to do with PHP and web programming in general. Besides, you ought to get credit for the posts. 🙂

                    Thanks for the help, I will keep this thread posted on any progress I make.

                      Hey, just came across this link (ubuntu pocket reference.) May be of use to you. Good luck! Also, that pi.comet library looks pretty good after looking into it more.

                      Good luck!

                        Thanks, it seems to be that the problem isnt coming up with a comet instance but rather a cross compatible version. I was able to do them with longpolling iframe to get it on ff and ie/opera but each browser presented its bug as well and it was taxing on the system

                          Yeah, I know from personal experience that FF will run up memory like mad for stuff like this 🙁

                            bretticus wrote:

                            Yeah, I know from personal experience that FF will run up memory like mad for stuff like this

                            Turn off its fastback cache?

                              Weedpacket;10900943 wrote:

                              Turn off its fastback cache?

                              Thanks for the tip. This will certainly improve my development from now on. Unfortunately, Firefox is tuned the way it is by default for everyone else. Good thing the client I'm doing AJAX work for is mandating that users use Internet Explorer (intranet project.)

                                I'm looking into socket and listening ports for changes which I can parse into commands.
                                APC is definately a great + thanks for the tip I got it installed and was playing with it today it really does instantly expand one's arsenal

                                  I have made a great deal of progress using socket server for push.
                                  However a big problem I ran into before and now is cross browser implimentation of FLUSH()
                                  I am having a great deal of trouble trying to flush data to Opera and Google Chrome.
                                  my headers

                                  set_time_limit (0);
                                  @apache_setenv('no-gzip', 1);
                                  @ini_set('zlib.output_compression', 0);
                                  @ini_set('implicit_flush', 1);
                                  for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
                                  ob_implicit_flush(1);
                                  echo str_repeat(" ",1500); 
                                  
                                  .... code cod ecode
                                  flush() 
                                  /// NADA
                                  

                                  are more then enough pad for IE and ff but it seems I'm still having problems specially with chrome that although built with comet in mind seems to be very buggy still. Anyone experience with succcessful flushing to those browsers?

                                    Okay here is what I've got

                                    I have a pretty decent socket server that can take multiple connections and buffer thigns to the appropriate client (although my code just talks back to the client at the moement that submitted thereqeust)

                                    I'm a little lost to be honest in how the actual communication is being handled and who is doing what. Here is my server called from CLI onlinux

                                    $address = '127.0.0.1';
                                    $port = 7503;
                                    
                                    
                                    function onChange ($clients, $socket, $input, $bytes)
                                    	{
                                    		foreach($clients as $client) 
                                    			{
                                    				if (trim($input) == "exit")
                                    					{
                                    						socket_close($client);
                                    						//socket_shutdown($sock);
                                    						exit(); 
                                    					}	
                                    					else 
                                    					{
                                    						//echo "user ".$client." says ".trim($input)."<br>"; 
                                    						socket_write($client, "<script>window.parent.add('".$client." says ".trim($input)."');</script>"); 
                                    						//flush(); 
                                    					}	
                                    			}
                                    	}
                                    // Create a TCP Stream socket
                                    $sock = socket_create(AF_INET, SOCK_STREAM,  SOL_TCP);
                                    // Bind the socket to an address/port
                                    socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
                                    socket_bind($sock, $address, $port) or die('Could not bind to address');
                                    // Start listening for connections
                                    $listen = socket_listen($sock, 5);
                                    //socket_set_nonblock($sock);
                                    $all_listen=array($sock); 
                                    while(true)
                                    	{
                                    
                                    	$changed_sockets = $all_listen;
                                    	$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL);
                                    
                                    	foreach($changed_sockets as $socket)
                                    		{
                                    			if ($socket == $sock)
                                    				{
                                    					if ($client = socket_accept($sock)) array_push($all_listen, $client); 
                                    					else echo $socket." cannot connect"; 					
                                    				}
                                    				else 
                                    				{
                                    					$bytes = socket_recv($socket, $buffer, 2048, 0);
                                    					if ($bytes == 0)
                                    						{
                                    							$index = array_search($socket, $all_listen); 
                                    							unset($all_listen[$index]); 
                                    							socket_close($socket); 
                                    						}
                                    						else
                                    						{
                                    							$ALL = $all_listen; 
                                    							array_shift($ALL); 
                                    							onChange($ALL, $socket, $buffer, $bytes); 
                                    						}						
                                    				}
                                    
                                    		}
                                    }
                                    

                                    here is my listener on the site

                                    $host="127.0.0.1";
                                    		$port = 7503;
                                    
                                    
                                    	/* works but ends connection */
                                    	$sock = socket_create(AF_INET, SOCK_STREAM,  getprotobyname('tcp'));
                                    	//socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);
                                    	socket_connect($sock, $host, $port); 
                                    	socket_set_option($sock, SOL_SOCKET, SO_BROADCAST,2050);
                                    	while(true)
                                    		{
                                    		$recieve = socket_read($sock, 1024); 
                                    	echo $recieve; 
                                    		}
                                    	socket_close($sock); 
                                    

                                    its doing well for the client to get is own feedback back
                                    I'm going to USE APC to assing users to socket resources and distrubte the messages however the browser isnt getting back any information from the client.
                                    I'm hoping to get some help because although its working 50% there i'm still a bit confused in the proccess and therefore have trouble troubleshooting

                                    Any Suggestions

                                    EDIT::: HAHAHA
                                    Nevermind I forgot to flush, dir

                                      22 days later

                                      I know you have this figured out, but for true data push you may want to evaluate other technologies like RTMP in the future. Red5 is an open source project that supports the publish/subscribe architecture over RTMP.

                                        Write a Reply...