0 Shares 2546 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
1666 views
CDN
1666 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
7947 views
CDN
7947 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…

Effects of the New-Generation Business World on Human Resources
Uncategorized
10 views
Uncategorized
10 views

Effects of the New-Generation Business World on Human Resources

Mujde Karakaya - September 22, 2020

We have been discussing lately how the business processes will be digitalized and due to the pandemic, we have witnessed that a process can digitalize even within…

E-commerce API Caching And Mobile Apps (Why You Need API Caching)
CDN
99 views
CDN
99 views

E-commerce API Caching And Mobile Apps (Why You Need API Caching)

Tushar Sonal - September 18, 2020

E-commerce API Caching And Mobile Apps Nowadays, it is typical for e-commerce platforms to use several different APIs to deliver wide-ranging functionality to their visitors. Application Programming…

The Right Way To Build Your Own CDN: Getting Started
CDN
227 views
CDN
227 views

The Right Way To Build Your Own CDN: Getting Started

Tushar Sonal - September 7, 2020

Here Is The Right Way To Build Your Own CDN Today, users expect fast and seamless website experiences. The performance needs to be superb and smooth. Businesses…

Most from this category

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

The Ultimate CDN (Content Delivery Network) Guide

Nadia Benslimane - August 3, 2020
The Essential CDN Glossary
CDN
7947 views
7947 views

The Essential CDN Glossary

Nadia Benslimane - November 21, 2019
This is WebP and This How To Use It
CDN
1177 views
1177 views

This is WebP and This How To Use It

Tushar Sonal - August 13, 2020