
#Php simple websocket install
The problem is that you need to compile and install the Swoole extension via PECL, which is something not everyone is keen to do, specially in Windows considering you’ll need to use WSL2 to actually use it. You can think about it has having NGINX inside your code, but with more granular control. This extension allows to runs the Web Server inside PHP. On the other hand, Swoole is itself an extension that you must load into PHP. It even supports gRPC, metrics, and braindead-easy configuration. It’s a very clean implementation that soon should have friendly support for receiving WebSockets connections and passing them to PHP. PHP itself is unaffected, there is no need to add external dependencies and extensions. It’s a web server binary that connects itself to PHP and load-balances it without bringing down the PHP runtime every time a request is served, but instead, it keeps the runtime alive. Roadrunner is simpler to understand as it can be considered total replacement of NGINX or Apache.

The thing with Swoole and Roadrunner is that these are a kind of different beasts, each with its own way to make PHP faster. From the most used, there is Ratchet that uses ReactPHP behind the scenes, while Amp has its own mature WebSocket Server.Īfter choosing your poison, you will be mostly preoccupied of doing something with the Client connected and the message that go back and forth the server using a single persistent PHP instance. Luckily for us, there are already some libraries that thought on that and offers their own WebSocket Server.

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. That’s in a nutshell, as the details deserve a whole article on its own. 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.Ĭouple that with an async library like ReactPHP or Amp, among the most used these days, and you can have a connection handler that doesn’t block the whole application while waiting for the next connection being made. The thing is, PHP has everything it needs to work with WebSockets. ReactPHP will ask for the user to set a “driver”, while Amphp will use them automatically in that order.
#Php simple websocket generator
There is an interesting small book about generators and how these can work to make code asynchronous.įor those wanting performance, these libraries can “bypass” the Generator performance penalty and directly use libraries that control events themselves, like libuv, libevent and libev. This example is simple and will return “bar foo” if you run it. Just a simple approach to almost-async functions. You can generate a loop over multiple “callables” registered inside a value that always returns the result of the callable in the stack: Generator are the key for Concurrency, a technique used in some programming languages, like Javascript.

It’s like the Fiber less-friendly cousin. In a nutshell, Generators are functions that can iterate over an “unknown” and return values as these are received, by just simply pausing the function execution and waiting for the next value. Some time ago, some folks found that PHP could handle some sort of concurrency through using Generators.

I’m always been very vocal about PHP being used for thing that is not suited for, like holding socket connections, when the nature of the language and the runtime itself is single-threaded, or “blocking” if you say.īut, 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. One thing I had to implement against my will was a Websocket server… in PHP.
