0

I would like to know if someone could explain to me how to build a real-time application with Symfony?

I have looked at a lot of documentation with my best friend Google, but I have not found quite detailed articles.

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).

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 !

Peter Artoung
  • 224
  • 5
  • 20

2 Answers2

1

Every useful Symfony application does some form of I/O. In traditional applications this is most often blocking 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.

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.

One solution is rewriting everything to non-blocking I/O, but then you'd no longer be using Symfony. You might be able to reuse some components, but only those not doing any I/O.

Another solution is to use RPC and queue WebSocket requests into a queue. The intermediary can be written using non-blocking I/O only, it doesn't have much to do. It basically just forwards WebSocket messages as RPC requests to a queue. 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. 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. 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.

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.

You can find a working implementation with BeanstalkD as queue in kelunik/rpc-demo. src/Server.php is just for the demo purpose and can be replaced with a HTTP server at any time. To keep the demo simple it uses a single WebSocket process but that can be changed as outlined above. You can start php bin/server and php bin/worker, then use telnet localhost 2000 to connect and send messages. It will respond with the same message but base64 encoded in the workers.

The mentioned demo is built on Amp, but the same concepts apply to ReactPHP as well.

kelunik
  • 6,750
  • 2
  • 41
  • 70
0

In this issue of the official Symfony repository you may find comments and ideas about this: https://github.com/symfony/symfony/issues/17051

Javier Eguiluz
  • 3,987
  • 2
  • 23
  • 44
  • Thank you, But good as I was expecting for the moment Symfony does not propose a simple method for a real-time application – Peter Artoung Mar 12 '17 at 18:06