0 Shares 665 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
6011 views
CDN
6011 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…

A Cheat Sheet To Delivering A High Streaming QoE
How To
154 views
How To
154 views

A Cheat Sheet To Delivering A High Streaming QoE

Nadia Benslimane - March 31, 2020

Free Download: A Cheat Sheet For A High Streaming QoE As we have mentioned in a previous article, we can't help but notice how OTT subscription services…

Laravel Migration Operations and Common Errors
Software Learning Center
185 views1
Software Learning Center
185 views1

Laravel Migration Operations and Common Errors

Tolga Karabulut - March 30, 2020

Common Errors Related To Laravel Migration This week, I will talk to you about migration with Laravel and some common Laravel migration errors. But first, what is…

The Impact Of Coronavirus On The Streaming Industry
OTT
2117 views
OTT
2117 views

The Impact Of Coronavirus On The Streaming Industry

Tushar Sonal - March 23, 2020

How Is Coronavirus Affecting The Streaming Industry? The unprecedented public health crisis, that the coronavirus outbreak is, has been declared a pandemic by WHO, on March 11.…

One Response

Most from this category

The Essential CDN Glossary
CDN
6011 views
6011 views

The Essential CDN Glossary

Nadia Benslimane - November 21, 2019
Shared vs Private SSL: What Is Best For You?
How To
577 views
577 views

Shared vs Private SSL: What Is Best For You?

Nadia Benslimane - March 11, 2020
What On Earth is Edge Intelligence?
Tech News
902 views
902 views

What On Earth is Edge Intelligence?

Tushar Sonal - February 28, 2020
A Deep Dive Into AKSELA
CDN
724 views
724 views

A Deep Dive Into AKSELA

Nima Najaflou - February 21, 2020