blog

  • Home
  • blog
  • Can PHP Be Even Faster? Light-Speed with the Blink Framework

Can PHP Be Even Faster? Light-Speed with the Blink Framework

Lumen, Silex, Slim, etc. you’ve heard of all of them. In this article, we’ll take a look at a newcomer called Blink.

Abstract image representing speed

Blink was built to improve high performance applications that consume a lot of server resources, and it uses the Swoole PHP extension to achieve this goal. As an example, we will build a Blink powered notejam for our demo. Let’s get started.

Installing The Swoole PHP Extension

We mentioned earlier that Blink relies on the Swoole extension for better performance.

You can install the Swoole extension from source on Github. Follow this article if you don’t know how to do it.

The alternative way to install it is by using the PEAR package manager, which is the approach I’ll take. I will be using an Ubuntu 14.04 image with the LAMP setup for this article, configured via Vaprobash – to get this exact setup, refer to the Vagrantfile in this project’s final repo. We could’ve used Homestead improved to easily set up the environment, but the Swoole extension is only compatible with PHP 5 for now, and Homestead Improved uses PHP 7 by default.

# update repositories
sudo apt-get update 

# Install pear manager
sudo apt-get install php-pear

# PHP source to compile the extension
sudo apt-get install php5-dev

# Used to install package dependencies
sudo apt-get install libcurl3-openssl-dev

# Installing Swoole
sudo pecl install swoole

The current Swoole version is 1.7.22-alpha and it’s not compatible with PHP 7, but there is an ongoing effort to make this happen in the next minor version.

After the commands above execute, we’re asked to add the extension=swoole.so line to php.ini. You can make sure that the extension is loaded by listing the available PHP modules command.

php -m | grep 'swoole'

Blink is available on Packagist, so we can use Composer to create a new project.

composer create-project --prefer-dist blink/seed

After all the dependencies have been loaded, you can run the server from the command line.

php blink server serve

This command will run an interactive shell for logs. However, you may use the php blink server start/stop/restart commands to manage the server.

We can make sure that everything is working as expected by visiting port 7788 of our local server:

Hello world

Configuration

While Blink doesn’t provide an option from the command line to specify the port, we can use the config/server.php file to change it.

// src/config/server.php

<?php
return [
    'class' => '\blink\server\SwServer',
    'bootstrap' => require __DIR__ . '/../bootstrap.php',
    'host' => '0.0.0.0',
    'port' => 8080,
];

Now, restart your server and check the browser using the new port. The config folder also holds configuration for our app and services.

Continue reading %Can PHP Be Even Faster? Light-Speed with the Blink Framework%

LEAVE A REPLY