eventhub/eventhub

An easily asynchronous event driven PHP framework.


Keywords
event-loop, asynchronous, php socket, push server
License
MIT

Documentation

What is it

EventHub is an asynchronous event-driven PHP framework with high performance to build fast and scalable network applications.

EventHub supports HTTP, Websocket, SSL and other custom protocols.

Requires

PHP 7.0 or Higher
A POSIX compatible operating system (Linux, OSX, BSD)
POSIX, PCNTL and EVENT extensions required

Installation

composer require eventhub/eventhub

// check environment
php eventhub/eventhub/check.php

// install pcntl extensions

// install posix extensions

// install event extensions
pecl install event
Include libevent OpenSSL support [yes] : no
echo extension=event.so > path to php.ini // eg: /usr/local/php/etc/php.ini
service php-fpm restart

Basic Usage

<?php

use EventHub\Worker;
use EventHub\Timer;
use EventHub\Connection\AsyncTcpConnection;

require_once __DIR__ . '/vendor/autoload.php';

$ssl_context = array(                                           // ssl context
    'ssl' => array(
        'local_cert'  => '/your/path/of/server.pem',
        'local_pk'    => '/your/path/of/server.key',
        'verify_peer' => false,
    )
);

$worker = new Worker('protocl://ip:port', $ssl_context);        // create server
$worker->count = 4;                                             // 4 processes
$worker->transport = 'ssl';                                     // enable ssl, wss: https:

$worker->onWorkerStart = function ($task) {
    $time_interval = 2.5; 
    $timer_id = Timer::add($time_interval, function () {
        echo "Timer run\n";
    });

    $service_connection = new AsyncTcpConnection('MyTextProtocol://service ip:80');
    $service_connection->onConnect = function ($connection) {
        $connection->send('Hello');
    };
    $service_connection->onMessage = function ($connection, $data) {
        echo "Recv: $data\n";
    };
    $service_connection->onError = function ($connection, $code, $msg) {
        echo "Error: $msg\n";
    };
    $service_connection->onClose = function ($connection) {
        echo "Connection closed\n";
    };
    $service_connection->connect();
};
$worker->onConnect = function ($connection) {
    echo "New connection\n";
};
$worker->onMessage = function ($connection, $data) {
    $connection->send('Hello ' . $data);
};
$worker->onClose = function ($connection) {
    echo "Connection closed\n";
};

// Run worker
Worker::runAll();

Protocols/MyTextProtocol.php

namespace Protocols;

/**
 * User defined protocol
 * Format Text+"\n"
 */
class MyTextProtocol 
{
    public static function input($recv_buffer)
    {
        // Find the position of the first occurrence of "\n"
        $pos = strpos($recv_buffer, "\n");

        // Not a complete package. Return 0 because the length of package can not be calculated
        if ($pos === false) {
            return 0;
        }

        // Return length of the package
        return $pos+1;
    }

    public static function decode($recv_buffer)
    {
        return trim($recv_buffer);
    }

    public static function encode($data)
    {
        return $data . "\n";
    }
}

Websocket server protocl: websocket Http server ptotocl: http TCP server protocl: tcp

Server commands

php start.php help

Commands: 
start           Start worker in DEBUG mode.
                Use mode -d to start in DAEMON mode.
stop            Stop worker.
                Use mode -g to stop gracefully.
restart         Restart workers.
                Use mode -d to start in DAEMON mode.
                Use mode -g to stop gracefully.
reload          Reload codes.
                Use mode -g to reload gracefully.
status          Get worker status.
                Use mode -d to show live status.
connections     Get worker connections.

LICENSE

The MIT License

Copyright (c) 2020 jiulin shaoditian@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.