What is the easiest way to implement a Comet serverside backend? - php

PHP as an Apache module or otherwise, would start one thread per pending request so it doesn't scale well.
Are Java and Python my only other options?
As a complete newbie to Python, is Twisted easy to use?

Twisted is very powerful but not easy to use, especially for a newbie.
tornado is another async server in Python, less general than Twisted (you would not use it for a network client for example) but simpler to use to implement servers, including Comet ones.

Investigate node.js. It's an evented server-side framework using JavaScript. It will not block communication with your browsers, since it's event-driven.

My experience, been absolutely newbie with Twisted is that you can implement Comet easily with It. I've used Athena, which is included in Nevow. Nevow is built on top of Twisted. It's third pary.
Give it a try.
Here is an example.

Related

Node.js vs PHP long-polling performance

I've read and heard that nods.js should be better and lighter at the server when doing long-polling then on a PHP-server. I'm doing a study on this and would like to measure the serverload at two simple long-polling applications to see the difference.
How could I measure this in a good way? I read something about apache bench but don't know if that works when doing long-polling. Also, I'm running on localhost.
Here are my two applications if someone want to take a look: https://github.com/furst/longpolling
NodeJS is a whole platform built with various packages and relies on various installed libraries to make "concurrency", sockets and event driven programming possible. This makes it able to do a lot of very interesting stuff, and it can do it quickly.
PHP is just a programming language, just like JavaScript is, and therefore PHP and NodeJS are not directly comparable. PHP has projects with scopes and intentions as NodeJS, they are just a little less known.
Ratchet: WebSockets for PHP
That is built on top of the socket component from ReactPHP, which together aims to be a much more fair comparison to NodeJS.

Html5 websockets ideal platform for php development

We are planning to build a vast web application that provides real-time data update and display(sth like stockmarket). There is a need for efficient server-client bidirectional communication. After research html5 web-sockets seem a must. However there are several issues regarding compatibility and fallback as well as server support. We need a simple and stable solution in php preferable and apache integration. We made some tests with phpwebsockets and pywebsockets but they seem not so stable. What would you propose as a more stable - tested solution, sth like kaazing maybe but in php? Thank you in advance.
What you want to use is Socket.IO, which takes care of all the cross-browser issues and provides seamless fallbacks for the older browsers. Socket.IO was made to be used with Node.js, but can now be used with a number of different server-side languages.
However, I would NOT recommend using Web Sockets with PHP. Because PHP is not designed for long running bidirectional communication and will cause an entire Apache process/thread to lock up with each new connection.
I would highly recommend using a language like Node.js on the server-side, which can easily handle thousands of long running connections without any problems.
Did you try COMET?
Theres a lot of sample of COMET+PHP apps on web.
http://www.zeitoun.net/articles/comet_and_php/start
http://ajaxian.com/archives/comet-with-php
http://www.phpclasses.org/blog/post/58-Responsive-AJAX-applications-with-COMET.html
Nodejs for two reasons:
1: You can use the same language on both client and server, thus more code re-use.
2: The built in event loop makes javascript ideal for those "do a tiny bit of work and then sleep for 20 seconds" situations.
You have to love javascript to take it on the server though.
I would probably go with Node.js. While I love javascript, I'm not drunk the Node.js cool-aid. (Fair warning…)
But Node.js allows you to use Socket.io - and that is what you want to be using to make your real-time communication work seamlessly on "all" systems. Communication between PHP and Node.js can be handled through sockets, a database or some other insane stuff.
WebSockets are not well supported, plus there is no stable php implementation.
Have you considered using long-polling/COMET? It will work across all browsers.

Is Haxe really good for php-targetting server side development?

I have such a conception, to build a simple web application using some php/mysql hosting service. I'd also like to create a local version of that app. The local version should work similarly as the online one (i.d. using browser as the ui) and have the ability to communicate with the server. I want it to be as lightweight as possible.
Professionally, I'm a c#/.net programmer. I also have some experience with c/c++,javascript, python and java. I know very little about php, and honestly, I don't like the feeling coding in that language, hence, don't have much willingness to dig into it. .Net, python or java they're all too unwieldy, I can't force the users to install any of them. So I thought about haxe's multi-targeting. If I were to code the project in haxe (it seems pretty straight forward to learn, for some one with my background), I might use the same logic on both side of the server and the client, by targeting both php and neko.
As a novice, I have some doubts. I'm not sure if haxe for php alreadly mature enough for this kinda job. I know haxe is a cousin of ActionScript, it primarily targets AS, and because the author is also the inventor of nekoVM, therefore, these two targets should be fine. But uh, how about php? Is it really good for that as well? Any hints or suggestions? Is haxe's builtin libraries ok for a simple web app or should I use a framework? Does neko have a httpRequest functionality at all?
Much gratitude in advance.
The straight answer is that Haxe/PHP seems to really fit well for you. That because of you background. I used Haxe/PHP in several projects (it happens that I am also its author) and speed has never been an issue for me. If there are bottlenecks usually there are also ways to optimize those portions for better performances, and if in the end you application scales to billions of users you can always opt to switch to neko or a C++/cgi combo.
About the frameworks there are a few options too: Web "frameworks" for Haxe to deploy in a PHP environment?

implement Comet with erlang and use it for PHP application

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?
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.
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.
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 (more info here).
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.
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
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.
You should look into Yaws. It's an Erlang web server that's been around for over a decade, is extremely stable and still under active development and maintenance, and supports long-polling, PHP applications, WebSockets, and much much more — pretty much everything you could want.
The Yaws sources are on github and its mailing list is here.
Try Chicago Boss framework here ... using that you don't have to know the nuts and bolts of the thing called OTP (which actually very easy, powerful and battle proved), because the designer of Chicago Boss, managed to encapsulate it nicely... according to the tutorial.
I'm learning it right now, after learning OTP.

Which Language has better Threading functionality? PHP or Ruby

I am creating a web application which requires threading and I am trying to figure out which langauge between PHP and Ruby has better threading functionality and better performance.
Even if not in built, some easy work arounds or add-ons.
I would have to say Ruby, since Ruby actually supports it. PHP does not.
With PHP, you can create new processes (which is a bad idea) or write multiple web services and use curl_multi_* functions to accomplish some things, but threading is not a feature of PHP.
These are probably the two worst languages to choose if you want threading, but if you really want one of these two, I guess Ruby can do it. Better go with JRuby, though. The JVM does very excellent threading.
(Or go with Groovy, which is basically Java with lots of Rubyisms.)
PHP does not normally do threading.
PHP currently has no support for the explicit use of threads; your PHP server may or may not use threads to serve different HTTP request ( the Zend engine does, I believe), but there are no facilities to create or coordinate threads via PHP code.
PHP does not have threading (good thing IMO).
Ruby has, but in 1.8 it has green threads, where in 1.9 it has a GIL. What this means (in the case of MRI and YARV - the 1.8 and 1.9 main Ruby implementations) is that 2 threads cannot run at the same time (in both cases) and you can't take advantage of multi-core processors.
You can use processes in both languages to overcome those limitations.
Neither may be good way to achieve what you are trying.
Consider looking at Gearman for asynchronous processing of "jobs". If your are looking to loosely couple your service with other services then you need a messaging service such as RabbitMQ.
It seems like Gearman will fit your needs.

Categories