0 Shares 1817 Views 1 Comments

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 Essential CDN Glossary
CDN
7419 views
CDN
7419 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…

What’s The Relationship Between CDN Content-as-a-service ?
CDN
1014 views
CDN
1014 views

What’s The Relationship Between CDN Content-as-a-service ?

Tushar Sonal - June 12, 2020

The Relationship Between CDN and Content-as-a-service What is the Content as a service? The Content as a Service (CaaS) or Managed Content as a Service (MCaaS) is…

Webinar Recap- The State Of Private CDN In 2020
CDN
607 views
CDN
607 views

Webinar Recap- The State Of Private CDN In 2020

Tushar Sonal - June 9, 2020

Introducing - Our Webinar Hosts Dan Rayburn - Streaming Industry Expert, Conference Chairman at NAB summit Serkan Sevim - CEO, Medianova We are going to discuss the…

How Does a CDN Help You In Your Website Security
CDN
798 views
CDN
798 views

How Does a CDN Help You In Your Website Security

Tushar Sonal - June 2, 2020

The Role Of  a CDN in Website Security Today, websites need to be consistent in delivering high performance to visitors. Websites have become an essential aspect of…

One Response

Most from this category

The Essential CDN Glossary
CDN
7419 views
7419 views

The Essential CDN Glossary

Nadia Benslimane - November 21, 2019
Bandwidth Usage Forecast with Machine Learning
CDN
1384 views
1384 views

Bandwidth Usage Forecast with Machine Learning

Ahmet Haydar Ornek - May 13, 2020
Medianova is Getting Ready for HTTP/3
Tech News
1189 views
1189 views

Medianova is Getting Ready for HTTP/3

Aykut Teker - April 30, 2020