2

I have been in search of making live websites by using PHP. (COMET) I have been searching for a very long time already. (around a month) I have even checked some PHP chat scripts and used on my webserver, but I had some problems on most of them. (will be explained)

So far, most of the people tells the same thing: PHP isn't the best language could be used with COMET. The chat/comet examples are hacky at best.

I am asking this because, I want to have some features on my websites, like allow logged in people to chat with each other. That is where I need an alive connection to PHP pages. I am also planning on making a browser based game, and an alive connection will still be a must!

AJAX was a rare thing 2 years ago, shined with Facebook. Now pretty much everyone uses it, and it became a standard on web development. Now, the COMET based websites are growing. Youtube, Google+, Facebook, EA's Battlelog and such. I believe I should learn how to use it.

Okay, here are my questions. (Some of the information at below are the ones I gathered while searching on Google. Not sure if they're correct or not.)

  1. Some languages like Python have special web servers designed for this job. I believe one of them is called Tornado Web Server. Developed and configured to simulate thousands of alive connections. I believe there is no such option in Appserv, but some people told NGINX can handle it with a decent configuration. Is it true? What configurations should be made? Is there any PHP web servers for this job?

  2. Some of the most suggested things are:

    a. Using APE.

    b. Using Socket.IO

    c. Node.js

    Is there any way we can use them with PHP? Which one is the most promising? Could you give some guidance on them? Is there anything else than these?

  3. I have used a comet chat script. Basically, you kept querying database and output the result with flush() to browser by sleeping it with usleep() in a loop. (This one became hard to understand so I will provide a quick code)

    while(true) { // query database // output the result // flush the browser // sleep for few seconds to lower cpu load } 

usleep() function basically destroyed my web server on Windows based operating systems. Is it normal to rely on usleep() on comet applications which runs on windows based OS? I mean, is there even a way to "sleep" PHP scripts? No matter what I do, CPU load goes to %100 on both WIN and UNIX servers.

  1. Is PHP "really" that weak on this area? Should I give up with PHP and focus on other languages? If so, which language would you suggest? (That language should be promising. For example, there is no much use of AS3 after HTML5 addition, and AS3 is more likely to die soon while JS shines.)

  2. What is WebSync? Can it be used with PHP?

Please bear in mind that I need to use COMET to make following applications:

  • A chat script, where logged in players will be able to chat eachother.
  • A browser based game. I already use JSON/AJAX and things like that when coding, but to receive opponents steps, I should pull the data, so an alive connection is needed. (Don't imagine advanced games, I am speaking about games like chess at best.)

I would appreciate if you can give me a short guidance. After all, I have been getting confused day by day since there are SO MANY random articles on internet. People even call setInterval() as COMET, so it is a complete mess.

5
  • If you're wanting to do real time applications, I would suggest node.js :) Commented May 3, 2012 at 21:52
  • 1
    me.veekun.com/blog/2012/04/09/php-a-fractal-of-bad-design gives a good summary on php ;P Commented May 3, 2012 at 21:52
  • Should I give up with PHP? You mean in general? Definitely not. It is perfectly acceptable to use this as your main language, whilst using something else more suited to the task in hand. For Comet, use something simple and lightweight that implements threading, and can talk to your site's database. Commented May 6, 2012 at 11:57
  • There are some interesting questions in your post, but "What is WebSync" is something that could be researched before asking here. Search for "WebSync with PHP" on the web if you want to know if it can be used with PHP :). Commented May 6, 2012 at 11:59
  • @tehlulz - that article is interesting, and it makes some fair points. But I'm not sure I like the snarky tone of it, nor the predictable bun-fight in the comments. Can't we all just learn to respect each others' tools? Commented May 6, 2012 at 12:19

2 Answers 2

1

There needs to be some clarification here. What you're looking for is not language specific per se. If you wanted to achieve the comet functionality with PHP you'd be relying on the Web Server (e.g Apache) to do the HTTP streaming. Also you need to understand that this is not supported in HTTP versions prior to HTTP/1.1. With that said, if you want to have a non-blocking event based web server, you're better off looking at Tornado and Node as suggested.

Sign up to request clarification or add additional context in comments.

Comments

0

Comet is a programming technique that enables web servers to send data to the client without having any need for the client to request it This technique will produce more responsive applications than classic AJAX The user must create a request (for example by clicking on a link) or a periodic AJAX request must happen in order to get new data fro the server.

but it's create lots of traffic on your web server. If you want to build chat application in PHP use pusher which is a third party service and easy to use. here is a link for pusher https://pusher.com/tutorials/realtime_chat_widget

the second suggestion is use ratchet for creating a chat application. here is link for ratchet http://socketo.me/docs/hello-world

i hope it will help you

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.