PHP WebSocket server
Clone or download
Latest commit 2c5972f Aug 8, 2018

PHP WebSocket server

Build Status Version Downloads License

Simple and multifunctional PHP WebSocket server

ollyxar websockets

Live chat example



ollyxar websockets performance

Installing WebSockets

The recommended way to install WebSockets is through Composer.

# Install Composer
curl -sS | php

Next, run the Composer command to install the latest stable version of WebSockets:

php composer.phar require ollyxar/websockets

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

Simple WebSocket server


use Generator;
use Ollyxar\LaravelAuth\FileAuth;
use Ollyxar\WebSockets\{
    Handler as BaseHandler,

class MyHandler extends BaseHandler
     * MyHandler constructor.
     * @param $server
     * @param $master
    public function __construct($server, $master)
        parent::__construct($server, $master);
        echo "I'm: #{$this->pid}\n";

     * @param $client
     * @return Generator
    protected function onConnect($client): Generator
        yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([
            'type'    => 'system',
            'message' => 'User {' . (int)$client . '} Connected.'

     * @param $clientNumber
     * @return Generator
    protected function onClose($clientNumber): Generator
        yield Dispatcher::async($this->broadcast(Frame::encode(json_encode([
            'type'    => 'system',
            'message' => "User {$clientNumber} disconnected."

     * @param string $message
     * @param int $socketId
     * @return Generator
    protected function onClientMessage(string $message, int $socketId): Generator
        $message = json_decode($message);
        if (!empty($message)) {
            $userName = $message->name;
            $userMessage = $message->message;

            $response = Frame::encode(json_encode([
                'type'    => 'usermsg',
                'name'    => $userName,
                'message' => $userMessage

            yield Dispatcher::async($this->broadcast($response));

User validation

Base Handler has own method to validate user to be logged in. By default it is always return true. You should provide your own implementation of method to authorize users.

     * Use this method for custom user validation
     * @param array $headers
     * @param $socket
     * @return bool
    protected function validateClient(array $headers, $socket): bool
        return true;


 * Lets start our server
(new Server('', 2083, 6, true))

Nginx reverse proxy

# choose location path
location /websocket/ {
    # use same host and port from your settings!
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;

Realtime testing (logging)

The server has internal logger that can output info into console. All that you need is just to enable logging before launching server.


output logging

Communicate with server outside the wss protocol

use Ollyxar\WebSockets\Server as WServer;
use Ollyxar\WebSockets\Frame;

$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
socket_connect($socket, WServer::$connector);

$data = new stdClass();
$data->type = 'system';
$data->message = 'hello World!';
$msg = Frame::encode(json_encode($data));

socket_write($socket, $msg);