views:

282

answers:

5

I'm building a PHP web application and I've reached a point that I need to build a Comet server because I need to update my users' whenever a new data is available (pretty much like FB). I've spent so much time searching the web and I've come to a conclusion that the best way to build Comet server is to build it with erlang. Also I've found that apache-php is not a good combination for doing that because the process per request issue.So, I have to build a lightweight http server for comet application.

I'm totally newbie in erlang world but I'm thinking of implementing Comet server in erlang and make it to function as interface for updating the clients only. For the rest of my web application functions, I still want to continue implementing them with PHP. So directing the requests of updating the clients to the erlang server and directing the other requests to apache-php server.

It seems very complicating. I need to know what's the best way to learn erlang for the sake of building Comet server and how to combine the two languages (erlang and php) to work together like when I have new info. to be pushed to the clients, I need to make the new changes available to Comet and then it pushes the info to the users. So how can I benefit from php and erlang and make them work together.

Sorry for the long explanation but I really need your help guys and any guidance you may give me to learn and implement what I want. Thanks a lot in advance.

EDIT: Should I consider learning Python and Twisted to accomplish what I want?

+2  A: 

You can learn Erlang pretty quickly, you should be able to use things like gen_server, gen_event and that sort of thing from OTP. The quickest way to learn Erlang should be to work your way through the documentation and examples at: http://www.erlang.org/doc/index.html.

For the communication between PHP and Erlang you can use sockets, fsockopen() and the rest on the PHP side and gen_tcp on the Erlang side. You can parse the Erlang terms sent trough the pipe from the PHP side (http://ftp.sunet.se/pub/lang/erlang/doc/apps/erts/erl_ext_dist.html).

I never used Erlang and PHP but I used Erlang and Python with some success, knowing PHP it should be pretty easy, just try to keep everything clean and keep the state on the Erlang side, using PHP only to generate the UI.

Dinu Florin
+2  A: 

Apache+php is indeed a bad technology for comet style applications. You can use a lot of other technologies that are closer to php though: Ruby, Python and Perl should all be usable. If you really want, you could probably write some kind of socket server in php aswell, but I would probably not bet on getting it to work out. That's not to say that Erlang isn't a good choice, but there are more mainstream alternatives.

If you don't want to use a mainstream language, be sure also to check out node.js, which runs some very impressive benchmarks. Plus you may already know a bit of javascript.

troelskn
I totally agree that It's better to learn mainstream languages and closer to PHP than learning erlang but is python really powerful and can handle many concurrent requests (100K+)? When you compare nodejs,python, which one is easier to learn and can handle such many request? Thanks for the help
codemaker
The problem with PHP is that you need to take up a process for each request. In most other languages, you can write a simple webserver and use a thread for each request, sharing the resources (eg. memory) between all the waiting requests. You can do that just fine in python. I don't know how the numbers stack up, so you should probably do some benchmarks, but I'm guessing they are fairly close.
troelskn
+3  A: 

It's definitely possible to do this with Erlang. One possibility would be to use long polling, which you can do with mochiweb. http://code.google.com/p/mochiweb/

Another idea is to use sockets. Until web sockets are supported by a reasonable number of browsers, you'll have to use a flash "bridge" to create a TCP connection, and use javascript to communicate with the server. Take a look at web socket JS: http://github.com/gimite/web-socket-js

Once you have this set up, you can communicate between your Erlang processes and PHP with something like this: http://www.math-hat.com/~zukerman/projects/php-erlang/

Then again, if you're still a newbie to Erlang, maybe you'll save time in the long run with Python and Twisted or Tornado.

Paul
Would you please guide me in how to implement Python+twisted to implement a comet server and is it possible to connect it with php application? Is Python+twisted able to handle many concurrent requests?
codemaker
Sorry, I haven't used Python/Twisted. I know it's built to handle many requests at once, and I'm sure there's a way for PHP and Python to communicate in the way you're talking about, but I don't know any way to do that off the top of my head.
Paul
+1  A: 

If you are considering Python and Twisted you can take a look at Orbited. They have very mature Comet implementation. You can make Orbited to communicate with your PHP application through STOMP protocol.

This article has a decent tutorial which will get you started with Orbited http://thingsilearned.com/2009/06/09/starting-out-with-comet-orbited-part-1/

To integrate your application with PHP you will need to google for PHP STOMP clients

+1  A: 

An addtional option is to use Nginx and it's push module (http://pushmodule.slact.net/)

This will allow you to use Comet from PHP without the need to learn a new language.

gdamjan
Don't you think that I'm going to face the same problem because PHP also has the process per request issue just like Apache?
codemaker