What is Swoole?

Swoole is a production-grade async programming framework for PHP. It is a PHP extension written in pure C language, which enables PHP developers to write high-performance, scalable, concurrent TCP, UDP, Unix socket, HTTP, WebSocket services in PHP programming language without too much knowledge about non-blocking I/O programming and low-level Linux kernel. You can think of Swoole as something like NodeJS but for PHP, with higher performance.

Swoole is widely used in many large enterprises in China, such as Baidu, Tencent, Camera 360 and so on. Swoole 2.0 even supports Coroutine,Swoole Coroutine is similar to Coroutine in other languages or frameworks. Swoole creates one coroutine for each request and schedule based on IO status.

Why run Laravel on Swoole?

Images below illustrate the lifecycle in PHP. As you can see, when you run PHP script every time, PHP needs to initialize modules and launch Zend Engine for your running environment. And your PHP script needs to be compiled to OpCodes and then Zend Engine can finally execute them.

However, this lifecycle needs to go over and over in each request. Because the environment for the single request will be immediately destroyed after the request process is completed.

In other words, in traditional PHP lifecycle, it wastes a bunch of time building and destroying resources for your script execution. And imagine in frameworks like Laravel, how many files does it need to load for one request? There's a lot of I/O consumption for loading files as well.

1. Introduction - 图1

1. Introduction - 图2

So what if we have a built-in server on top of Swoole, and all the scripts can be kept in memory after the first load? This is why we try to run Laravelon Swoole. Swoole can be a powerful performance booster and Laravel provides the elegant structure and code usages. That's a perfect combination!

Conclusion

Nevertheless, to be honest, this is not an easy job to integrate Swoole with Laravel. Laravel itself is not designed for structures like Swoole. And there are many slight changes in the internal code every time Laravel releases a new version. Please let me know if you find any bugs in a specific version.

After v2.6.6, this package no longer supports Laravel/Lumen 5.1 ~ 5.4.