0 Shares 3062 Views

Swoole Installation and Laravel

Swoole and Laravel

Hello again everyone, today I will try to tell you more about Swoole. It doesn’t matter whether you’ve heard of it before or not, because when you finish reading this article, you will understand why I decided to talk about it. So bear with me and let’s start!

First of all, What is Swoole?

swoole medianova

Swoole is an Asynchronous, Event Oriented Coroutine based concurrent network communication engine written in C and C ++ developed for PHP. We can say that it is a new era for PHP without getting to explanation. In this article, we will focus on its usage on Laravel.

System requirements

  • Operating System: Linux*, FreeBSD, macOS
  • Linux Kernel Version ≥ 2.3.32
  • PHP Version ≥ 5.3.10
  • GCC version ≥ 4.4
  • Cmake Version ≥ 2.4 **

* CentOS 6.2+ | Ubuntu 12+ | Debian 6+
** Required for Cmake Swoole Library to compile in C / C ++.
Recommended Environments are ≥ 14 and CentOS ≥ 7 for Ubuntu.


Setup

We can set up Swoole via Pecl. as follows;

#!/bin/bash
pecl install swoole

In this way, we ensure that the Swoole library is installed on our system. If we want to install it manually;

#!/bin/bash
cd /tmp
wget https://github.com/swoole/swoole-src/archive/v4.4.16.zip
cd swoole
phpize
./configure
make
swoole/module/swoole.so
sudo make install

After that, a library was added in our system. But we did not activate it. So now let’s do the activation process.

php -i | grep php.ini
sudo echo "extension=swoole.so" >> php.ini
php -m | grep swoole

The above blog makes a definition for Swoole into our php.ini file. You can check this by going to your php.ini file.

After all, it is now ready to use Swoole. But since we will talk about its use on Laravel, there are a few more steps we need to take, let’s go through them quickly.

Laravel and Swoole

Now that you have come here, I will not talk about creating a Laravel Project. You will find that it can be easily added with a single change in the directory where your project is located. Let’s add it;

composer require swooletw/laravel-swoole

We start by adding the swooletw / laravel-swoole package to our project via Composer, then | We go to our /config/app.php file and add our Service class to providers.

#config/app.php
[ 'providers' =>
    [
      SwooleTW\Http\LaravelServiceProvider::class,
    ],
]

Thus, we have added Swoole to our Laravel Project. Now let’s run our project and do our tests. We run the following code over Artisan CLI.

php artisan swoole:http start

If you get this message, our project is working.

Starting swoole http server… Swoole http server started: <http://127.0.0.1:1215>

Now we can make a few settings. First of all, let’s run on port 80 instead of port 1215. After installation, we have created swoole_http.php file in our Config folder. All of our Swoole related settings are only here for now.

‘port’ => env(‘SWOOLE_HTTP_PORT’, ‘1215’) instead

‘port’ => env(‘SWOOLE_HTTP_PORT’, ’80’),

At this point, we can clearly see

that this is not the right method. We add the following key to our .env file and run it again.

#! /.env
SWOOLE_HTTP_PORT=80
#!/bin/bash 
php artisan optimize:clear  
php artisan swoole:http start

Then our project started broadcasting on our 80 ports. We can now run a benchmark test. I share the sample outputs with you. I am writing my tests yet, and I will share them extensively here in another article. If you share your results and test conditions in the comments, you may have more ideas. Thanks in advance.

#! Nginx – PHP-FPM

Running 10s test @ http://127.0.0.1

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 ServerRunning 10s test @ http://127.0.0.1

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

Resources :

https://github.com/swooletw/laravel-swoole

https://www.swoole.co.uk

 

You may be interested

The Ultimate CDN (Content Delivery Network) Guide
CDN
2566 views
CDN
2566 views

The Ultimate CDN (Content Delivery Network) Guide

Nadia Benslimane - August 3, 2020

The Ultimate CDN Guide- Everything About CDN You probably know what CDN (Content Delivery Network) stands for. You may also be aware of its full definition, but…

The Essential CDN Glossary
CDN
8267 views
CDN
8267 views

The Essential CDN Glossary

Nadia Benslimane - November 21, 2019

Why Have We Decided To Create a CDN Glossary? Whether you are new to the world of CDN, or have been involved in it for years, there…

Live Streaming In E-commerce: Could It Be The Future of Retail?
e-commerce
545 views
e-commerce
545 views

Live Streaming In E-commerce: Could It Be The Future of Retail?

Tushar Sonal - November 6, 2020

E-commerce and Live Streaming: Where is the meeting point? Over the past decade, significant disruption has rolled-out transformative changes in retail and e-commerce. An interesting example is…

Black Friday 2020 and Covid-19: How Is The Game Changing?
e-commerce
1117 views1
e-commerce
1117 views1

Black Friday 2020 and Covid-19: How Is The Game Changing?

Tushar Sonal - October 21, 2020

How Is Covid-19 Going To Impact Black Friday 2020? Black Friday is around the corner again. It is a much-awaited festival for shopping enthusiasts and brands chasing…

Coronavirus and OTT: What Is The Current Situation?
CDN
984 views
CDN
984 views

Coronavirus and OTT: What Is The Current Situation?

Tushar Sonal - October 13, 2020

The Repercussions of Coronavirus on OTT Industry The OTT industry has significantly transformed how people have been used to consuming media content. In the 2000s, the explosion…

Most from this category