Source: darkghosthunter.medium.com

The reality of PHP WebSockets

Category: PHP, PHP, javascript, javascript

But, even If I didn’t wanted to and looked into Go and Rust for the implementation, I figured out that WebSocket on PHP wasn’t THAT bad to work with, but there were a few caveats. Some time ago, some folks found that PHP could handle some sort of concurrency through using Generators.

The stream_socket_server function in PHP allows to create a socket that returns whatever connection goes in, as it works like a Generator, along stream_set_blocking set as non-blocking.

Now that we have very “hacky” but stable way to make async calls on PHP — at least until Fibers come in PHP 8.1 at the end of the year — the next step is to hook into the socket server, and then upgrading the incoming HTTP Request connections to WebSockets connections, keeping them open in memory.

You can use and deploy a WebSocket server in a PHP server today.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners