简体   繁体   English

实时应用Symfony-使用什么技术?

[英]Real-time Apps Symfony - What technology to use?

I would like to know if someone could explain to me how to build a real-time application with Symfony? 我想知道是否有人可以向我解释如何使用Symfony构建实时应用程序?

I have looked at a lot of documentation with my best friend Google, but I have not found quite detailed articles. 我和我最好的朋友Google一起看了很多文档,但是没有找到非常详尽的文章。

I would like some more PHP-oriented thing and saw that there were technologies like ReactPHP / Ratchet (but I can not find a tutorial clear enough to integrate it into an existing symfony project). 我想要更多面向PHP的东西,并看到有类似ReactPHP / Ratchet的技术(但我找不到足够清晰的教程来将其集成到现有的symfony项目中)。

Do you have any advice on which technologies to use and why? 您对使用哪种技术有何建议?为什么? (If you have tutorial links I take!) (如果您有我要的教程链接!)

Thank you in advance for your answers ! 预先感谢您的回答!

Every useful Symfony application does some form of I/O. 每个有用的Symfony应用程序都会执行某种形式的I / O。 In traditional applications this is most often blocking I/O. 在传统应用中,这通常是阻塞I / O。 Even if it's non-blocking I/O, it doesn't integrate a global event loop that could schedule other things while waiting for I/O. 即使它是非阻塞I / O,也不会集成一个可以在等待I / O时安排其他事情的全局事件循环。

If you integrate Symfony into an existing event loop based WebSocket server, it will work with blocking I/O as a proof of concept, but you will quickly notice it isn't running fine in production, because any blocking I/O blocks your whole event loop and thus blocks all other connected clients. 如果将Symfony集成到基于事件循环的现有WebSocket服务器中,它将作为阻塞I / O的概念证明,但您很快就会注意到它在生产中运行不佳,因为任何阻塞I / O都会阻塞您的整个工作事件循环,从而阻止所有其他连接的客户端。

One solution is rewriting everything to non-blocking I/O, but then you'd no longer be using Symfony. 一种解决方案是将所有内容重写为非阻塞I / O,但是您将不再使用Symfony。 You might be able to reuse some components, but only those not doing any I/O. 您也许可以重用某些组件,但是只能重用那些不执行任何I / O的组件。

Another solution is to use RPC and queue WebSocket requests into a queue. 另一个解决方案是使用RPC并将WebSocket请求放入队列。 The intermediary can be written using non-blocking I/O only, it doesn't have much to do. 中介只能使用非阻塞I / O编写,没有太多的工作。 It basically just forwards WebSocket messages as RPC requests to a queue. 它基本上只是将WebSocket消息作为RPC请求转发到队列。 Then you have a set of workers pulling from that queue, doing a normal Symfony kernel dispatch and sending the response into a response queue. 然后,您有一组工作程序从该队列中拉出,进行常规的Symfony内核调度,并将响应发送到响应队列中。 The worker can then continue to fetch the next job. 然后,工人可以继续获取下一份工作。

With the second solution you can totally use blocking I/O and all existing Symfony components. 使用第二种解决方案,您可以完全使用阻塞I / O和所有现有的Symfony组件。 You can spawn as many workers as you need and you can even keep them alive between requests. 您可以根据需要生成任意数量的工作程序,甚至可以在两次请求之间保持活动状态。 The difference with a queue in between is that one blocking worker doesn't block the responsiveness of the WebSocket endpoint. 两者之间的队列的区别在于,一个阻止工作程序不会阻止WebSocket端点的响应。

If you want multiple WebSocket processes, you'll need separate response queues for them, so the responses are sent back to the right process where the client is connected. 如果需要多个WebSocket进程,则需要为它们提供单独的响应队列,因此响应将发送回连接客户端的正确进程。

You can find a working implementation with BeanstalkD as queue in kelunik/rpc-demo . 您可以在kelunik/rpc-demo找到将BeanstalkD作为队列的有效实现。 src/Server.php is just for the demo purpose and can be replaced with a HTTP server at any time. src/Server.php仅用于演示目的,可以随时用HTTP服务器替换。 To keep the demo simple it uses a single WebSocket process but that can be changed as outlined above. 为了使演示简单,它使用单个WebSocket进程,但是可以如上所述进行更改。 You can start php bin/server and php bin/worker , then use telnet localhost 2000 to connect and send messages. 您可以启动php bin/serverphp bin/worker ,然后使用telnet localhost 2000连接并发送消息。 It will respond with the same message but base64 encoded in the workers. 它将以相同的消息进行响应,但base64编码为worker。

The mentioned demo is built on Amp , but the same concepts apply to ReactPHP as well. 提到的演示基于Amp构建,但是相同的概念也适用于ReactPHP。

在本期Symfony官方存储库中,您可以找到有关以下内容的评论和想法: https : //github.com/symfony/symfony/issues/17051

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM