Websocket server which to choose? - php

I am developing a big PHP application in ZF2, and I need real-time interaction in this app, so websocket are my choose.
I have PHP, Mysql, Memcached. I need a websocket server. What would be the best choise.
Requiments of my websocket server:
support mysql, memcached and maybe gearman
Support https connections, very important
optionally support IE (but this is not important if loose benefits)
scalable
My problem is choosing from a PHP (wrench) websocket server or a based Node.js(socket.io) server.
Which should I choose?
If I use the PHP one I can use PHP code and ZF2 function, so easy to develop, but I do not have non-html5 browser support (do I have ssl support here?)
If I use socket.io I have all browser support and fallback, but here I need to re-write all function (in js) to work with mysql, memcached and so on...

WaterSpout Server claims to offer WebSocket or Long-Polling connections:
https://github.com/chrisnetonline/WaterSpout-Server
So this may be a solution that works in all browsers.
All solutions are scalable. The questions are really; how soon do you want to have to start thinking about scaling? When do you want to scale vertically and add additional resource to your server? When do you want to add another server (scale horizontally) and deal with synchronisation connection and state information between those servers? IMHO you'll be doing this sooner with a PHP solution.
If you mainly want to write code in PHP and you would prefer to keep things simple then I'd recommend you use a hosted realtime service as part of your application stack.
Some relevant posts (which I wrote):
Understanding Realtime PHP Apps
Building Realtime Web Apps with PHP

I'd recommend Node.js, because PHP is not the best solution for applications, which have to stay in memory for a long time (PHP leaks memory).
http://software-gunslinger.tumblr.com/post/47131406821/php-is-meant-to-die
I'm sure there are lots of reusable Node modules for you not to reinvent a bycicle.
Of course, if it works for you, you can use some PHP-powered daemon and restart it once in a while to control it's stability to some extent.

Related

PHP: use Golang to handle websocket connections

I have a fairly big web application build with SproutCore and PHP as backend. What I want now is to use websockets to update my client application in real time.
From what I know, PHP is really bad to handle persistent connections. So I've been thinking that I could use Go to handle the websockets connections and call my PHP scripts each time a request is received (this package seem to make it possible).
So my first question is, do you guys think it's a good idea (and a viable idea, I haven't been able to find people doing so) or should I stick with PHP ?
If I use Go to handle the websockets connections I've also been thinking that I could progressively move away from PHP to only use Go (since it is a lot faster than PHP). If I do that, I will have to be able to call some Go package from PHP. Can this be done with the PHP exec function ? Is there a better way ? And again, is that a good idea ?
Go is a natural fit for websocket servers. I've built websocket servers in Go and have been extremely happy with how it all worked out. I have one service handling 300k users a month on a Go websocket server and it barely uses 1% CPU of an Amazon AWS micro instance. Couldn't be happier.
Websockets really need event driven frameworks like Go and Node.js in order to maximize server resources. Forked web processes like PHP consume far more resources than an event driven framework.
If you need to call Go from PHP at some point, I suggest using API calls. Although exec would work too.
It is an old question, but my two cents on this subject...
There is a very good php library that does exactly what you are asking for - websockets. It is called Ratchet. I would not use node.js (over hyped) or go when php can do exactly the same thing and it is the language that I am most comfortable with. In majority of cases, little gain in performance over websockets is not worth switching stack.
Other useful links if you plan on using Ratchet:
Backend - Ratchet Examples
Frontend - Autobahn WS

How to integrate a scalable long-polling server with PHP?

I've been "recruited" so to speak to help work on a web project that is currently written in PHP with an Apache server. We would like to integrate a real-time (or at least something very close to it) chat feature. Scalability is a definite concern, and this type of work is definitely not my typical.
Everything I've read about creating such a chat feature requires the use of "long-polling" so the servers don't get rapidly overloaded and, well, crash. PHP and Apache are not conducive to implementing such a feature, so I've explored some alternatives, like Twisted Python for example.
The website has roughly ~7,000 lines of PHP (i.e., it'd pretty difficult to just straight switch languages for the entire thing), so my question is how can I manage this situation as far as trying to integrate python and setting up a separate server? Or, is this a very bad way to do this? Are there other alternatives that would be better suited? (Sadly many of the PHP Comet, or even AJAX, solutions I've found don't scale in the slightest. Note, the Apache server is not necessarily required; however, any server used must work with PHP and Python etc, short of having separate servers.)
Thanks for the help!
I would use Tornado on the server to write the chat application. Client server communication can then be over websockets. If you use SockJS on the client side you can also support older browsers via long polling. There are plenty of example chat clients written using Tornado. It's very simple to get started and it is wonderfully scalable. A chat server like this can be serving thousands of clients without showing any appreciable CPU activity.
This is an example, possibly a bit over engineered https://github.com/diggidanne/websocket-chat/blob/master/server.py

Are websockets suitable for use with PHP?

I have seen it mentioned in various places around the internet that HTML5 websockets do not work well with PHP, that PHP by it's nature is just suitable for use with them. On the other hand, I see multiple tutorials on using PHP with websockets and Ive noticed some PHP websocket implementation such as http://code.google.com/p/phpwebsocket/
So does anyone have any definitive information on using websockets with PHP. Are they usable with PHP, what are the advantages/disadvantages of using them with PHP as opposed to Java or Python, and why have I read numerous people saying they don't work well together?
The problem is that WebSockets are designed for long running threads/processes which each maintain multiple event-driven connections, whereas PHP (and it's Apache cohort) was designed around the short-lived single process procedural paradigm (eg. max_execution_time is commonly set to 30 seconds, and the session is single threaded).
That's not to say that it's impossible to write a WebSockets server implementation in PHP. I'm aware of at least one project exists that has done exactly this (but note, even this example gets run from the command line, not through mod_php). But it is likely that the PHP implementation of WebSockets is incompatible with the setup of the cheap/shared hosting where PHP is most commonly used.
So while it's possible to it in PHP, you end up having to run a separate server process (from Apache) anyway, and if you're on the sort of hosting that allows separate server processes then it's easier to write WebSockets code in something which is designed for event-driven programming.
If you're not planning to serve tens of thousands of concurrent duplex connections then it's likely you'd be better off using a combination of AJAX and SSE with your PHP back-end.
I recently tried phpwebsocket and it doesn't work at this time (php 5.4 and chrome) the code refers to a secondkey in the handshake that doesn't exist in rev. 13 of the websocket protocol I don't have the time to read the RFC to understand what's the matter.
It's sure that this solution is more elegant and reactive than AJAX with long polling but websockets are not stable at this time I think it would be more interesting to wait that the w3c announce it stable.

Using WebSocket on Apache server

With all the buzz around WebSockets, it's pretty hard to find a good walkthrough on how to use them with an Apache server on Google.
We're developing a plugin, in PHP (symfony2), which will run from time to time kind of a chat instance. And we find WebSockets more interesting, standard and quick than AJAX for this matter. The thing is, we don't have much sysadmin ressources in our group and we find hard to gather good informations on the following matters:
Can we run a WebSocket instance on a traditional Apache, dedicated server, and if yes, do you have useful links for us?
If we need to mod the server, what kind of tools would you recommend knowing that we are not too skilled in sysadmin so we can't afford to have a high maintenance b*** on this.
Thank you very much,
ps: we'll link back to your blog/site as we'll make a technical/informational post on our devblog about this part of our app.
Thank you again!
As #zaf states you are more likely to find a standalone PHP solution - not something that runs within Apache. That said there is a apache WebSocket module.
However, the fundamental problem is that Apache wasn't built with maintaining many persistent connections in mind. It, along with PHP, is built on the idea that requests are made and responses are quickly sent back. This means that resources can very quickly be used up if you are holding requests open and you're going to need to look into horizontal scaling pretty quickly.
Personally I think you have two options:
Use an alternative realtime web technology solution and communicate between your web application and realtime web infrastructure using queues or short-lived requests (web services).
Off load the handling of persistent connections and scaling of the realtime web infrastructure to a realtime web hosted service. I work for Pusher and we fall into this category.
For both self-hosted and hosted options you can check out my realtime web tech guide.
One path is to use an independent installed web sockets server.
For PHP you can try:
http://code.google.com/p/phpwebsocket/ or http://github.com/Devristo/phpws/
There are some other projects which you can try as well.
Basically, you need to upload, unpack and start running the process.
On the frontend, you'll have javascript connecting to the server on the specific port.
Most websocket servers have a demo which echoes back whatever it hears, so this is a good place to write some test code. You may even find a rudimentary chat implementation.
The tricky part is to monitor the web socket server and to make sure it runs smoothly and continuously.
Try to test on as many browsers/devices as possible as this will decide on which websocket server implementation you choose. There are old and new protocols you have to watch out for.
I introduced another websocket server: PHP Ratchet (Github).
This is better and complete list of client & server side codes and browser support.
Please check this link.
Another Path is to use a dedicated websocket server.
Try Achex Websocket Server at www.achex.ca and checkout the tutorials.
OR
If you really want Apache, check out Apache Camel. (but you have to set it up and its a bit more complicated than achex server)
http://camel.apache.org/websocket.html

socket.io + php + is this a realistic solution at this point in time

I have been reading about websockets and also about socket.io.
I understand websockets are not support but enough browsers yet to be realistically used.
socket.io has browser support but appears to need node.js. With my hosting solution I have some space on a shared server, very limited control over the server and have access to php but there is no node.js.
I have read that people can get socket.io to work with PHP.
Question: seeing I have very limited server access, not control over ports, command line etc is it realistic that I could get socket.io working with PHP or should I just stick with the AJAX calls (I'm currently using)?
Note: I've developed a chat solution and it appears it would be much more light weight if it was running with a socket solution.
Also - is there good security with Sockets.io?
thx
Trying to make PHP talk to socket.io I suspect will be a big task.
Certainly it's not a great idea to run websockets via a pre-fork or threaded apache. Any event based server should be OK if you can handle the number of PHP processes. A better approach would be to write an event based server in php. Like this one
i researched a few websocket server implementations. i read, that there is a way to use node.js in production. using apache many years, i also searched for a module to handle websockets within apache, but found only half-hearted stuff. nothing which seemed solid.
i stumbled upon yaws, which is a websocket capable server written in erlang, which has the advantages of beeing the right language for a highly parallel application such as the chat thing, and also because websockets should be well integrated within the main server.
now, i'm developing a php chat server, which uses a framework called ratchet. -> google for 'ratchet php'. they got a good tutorial.
i start it via the commandline, and although for you it should be possible to also start the ratchet server via a browser call, i would not recommend that.
so in your constrained hosting environment, i would stick to ajax.

Categories