5

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 ?

2 Answers 2

9

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.

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

4 Comments

Seem promising. So you're agree with me that calling a PHP script each time a request is received by the Go websocket server should be far better than handling the websocket connection all in PHP.
I also use Go to run my websocket servers (among them this. It's very efficient and clean. I think using PHP for this would really be a bad idea.
Calling a PHP script each time a request is received by the Go websocket server would almost certainly be a better solution. If any of your data is cacheable, you can cache it in the Go instance, for one. For two, you only use PHP resources long enough to answer a request so the overall number of PHP processes should be significantly less so long as the websockets are not constantly pushing new requests to PHP. Finally, you put yourself in a position to migrate all your PHP code to Go over time and realize enormous efficiency gains.
An alternative to exec is to use PHP-FPM with NGINX and some optcode cacher like APC. This allow to run your PHP code in fastest possible way plus you not bounded to localhost.
-1

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:

1 Comment

Ratchet is very poorly maintained and their examples don't work with the latest versions of PHP (5.5+). Although a good idea, with the current implementation and the huge array of dependencies it requires, it doesn't work very well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.