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 without too much knowledge of the 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.
Why run Laravel on Swoole?
The image below illustrates 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 for Zend Engine’s execution.
However, this lifecycle needs to go over and over again in each request. Because the environment created for a single request will be immediately destroyed after the request process is done.
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.
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 Laravel on Swoole. Swoole can be a powerful performance booster and Laravel provides the elegant structure and code usages. That’s a perfect combination!
Here are the main features of
- Run Laravel/Lumen application on top of Swoole.
- Outstanding performance boosting up to 30x.
- Sandbox mode to isolate app container.
- Support running WebSocket server in Laravel.
- Support Swoole table for cross-process data sharing.
Require this package with Composer:
$ composer require swooletw/laravel-swoole
This package relies on Swoole extension. Make sure you’ve installed Swoole before you use this package. Using this command to install it quickly:
pecl install swoole
After installing the extension, you will need to edit php.ini and add an
extension=swoole.so line before you use it.
php -i | grep php.ini # check the php.ini file location sudo echo "extension=swoole.so" >> php.ini # add the extension=swoole.so to the end of php.ini php -m | grep swoole # check if the swoole extension has been enabled
Visit the official website for more information.
Notice: Swoole currently only supports Linux and OSX. Windows servers are not able to use Swoole yet.
Then, add the service provider:
If you are using Laravel, add the service provider to the provider’s array in
[ 'providers' => [ SwooleTW\Http\LaravelServiceProvider::class, ], ]
If you are using Lumen, append the following code to
It supports package auto discovery. If you’re running Laravel 5.5, you can skip this step.
Up and Running
Now, you can run the following command to start Swoole HTTP server.
$ php artisan swoole:http start
Then you can see the following message:
Starting swoole http server... Swoole http server started: <http://127.0.0.1:1215>
Now you can access your Laravel application on
Test with clean Lumen 5.5, using MacBook Air 13, 2015.
Benchmarking Tool: wrk
wrk -t4 -c100 http://your.app
Nginx with FPM
Running 10s test @ http://lumen.app:9999 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 1.14s 191.03ms 1.40s 90.31% Req/Sec 22.65 10.65 50.00 65.31% 815 requests in 10.07s, 223.65KB read Requests/sec: 80.93 Transfer/sec: 22.21KB
Swoole HTTP Server
Running 10s test @ http://127.0.0.1:1215 4 threads and 100 connections Thread Stats Avg Stdev Max +/- Stdev Latency 11.58ms 4.74ms 68.73ms 81.63% Req/Sec 2.19k 357.43 2.90k 69.50% 87879 requests in 10.08s, 15.67MB read Requests/sec: 8717.00 Transfer/sec: 1.55MB
Learn MoreFiled in: Laravel Packages
Join 31,000+ others and never miss out on new tips, tutorials, and more.
- Senior Full-Stack Engineer
[REMOTE: USA Only] All Other Applications Will be Rejected Immediately"
- Senior Full Stack PHP Developer
Motto Design Studio
- 👉 Looking for Senior Back-End Laravel API Developer - Immediate (Full-time Contractor) 🎉
ApproveMe.com // The Document Signing Experience™
- Senior Full-Stack Developer
Remote, USA Only
- Senior Full Stack Engineer
Remote / Los Angeles, USA