英文:
PHP: use Golang to handle websocket connections
问题
我有一个使用SproutCore和PHP作为后端构建的相当大的Web应用程序。现在我想使用Websockets实时更新我的客户端应用程序。
据我所知,PHP处理持久连接的能力很差。所以我一直在考虑是否可以使用Go来处理Websockets连接,并在每次收到请求时调用我的PHP脚本(这个包似乎可以实现)。
所以我的第一个问题是,你们认为这是一个好主意(而且可行的主意,我还没有找到其他人这样做)还是应该坚持使用PHP?
如果我使用Go来处理Websockets连接,我还在考虑是否可以逐步放弃PHP,只使用Go(因为它比PHP快得多)。如果我这样做,我将需要能够从PHP调用一些Go包。这可以通过PHP的exec
函数来实现吗?有没有更好的方法?而且,这是一个好主意吗?
英文:
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 ?
答案1
得分: 9
Go是websocket服务器的天然选择。我在Go中构建了websocket服务器,并对其运行情况非常满意。我有一个服务在Go websocket服务器上处理每月30万用户,并且几乎不使用亚马逊AWS微实例的1% CPU。非常满意。
为了最大化服务器资源,Websockets确实需要像Go和Node.js这样的事件驱动框架。与事件驱动框架相比,像PHP这样的分叉web进程消耗更多资源。
如果您需要在某个时候从PHP调用Go,我建议使用API调用。虽然exec
也可以工作。
英文:
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.
答案2
得分: -1
这是一个老问题,但我对这个主题有一些看法...
有一个非常好的PHP库可以完全满足你的需求 - websockets。它被称为Ratchet。当PHP可以做到完全相同的事情,并且这是我最熟悉的语言时,我不会使用node.js(过度宣传)或go。在大多数情况下,通过websockets获得的性能提升不值得切换堆栈。
如果你计划使用Ratchet,其他有用的链接如下:
-
后端 - Ratchet示例
-
前端 - Autobahn WS
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论