PHPackages                             ollyxar/websockets - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. ollyxar/websockets

ActiveLibrary[Framework](/categories/framework)

ollyxar/websockets
==================

PHP WebSocket server

2.2.1(8y ago)311.9k7[1 issues](https://github.com/ollyxar/websockets/issues)1MITPHPPHP ^7.1

Since Oct 16Pushed 8y ago4 watchersCompare

[ Source](https://github.com/ollyxar/websockets)[ Packagist](https://packagist.org/packages/ollyxar/websockets)[ Docs](https://ollyxar.com/websockets)[ RSS](/packages/ollyxar-websockets/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (10)Dependencies (1)Versions (21)Used By (1)

PHP WebSocket server
====================

[](#php-websocket-server)

[![Build Status](https://camo.githubusercontent.com/f2ed71cf348696f7f06399f4e65218d5c31ec80627bcbc2548ba93bac7fab719/68747470733a2f2f7472617669732d63692e6f72672f6f6c6c797861722f776562736f636b6574732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/ollyxar/websockets)[![Version](https://camo.githubusercontent.com/aa39af9d4465e36fa11599e875797be3cd95b3e6b689572236a48d4dac945890/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c797861722f776562736f636b6574732f762f737461626c652e737667)](https://camo.githubusercontent.com/aa39af9d4465e36fa11599e875797be3cd95b3e6b689572236a48d4dac945890/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c797861722f776562736f636b6574732f762f737461626c652e737667)[![Downloads](https://camo.githubusercontent.com/aef90dd2962e896f23e1b0a3ce280b71456581b5610741091ab4ad8d87c3ec5a/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c797861722f776562736f636b6574732f642f746f74616c2e737667)](https://camo.githubusercontent.com/aef90dd2962e896f23e1b0a3ce280b71456581b5610741091ab4ad8d87c3ec5a/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c797861722f776562736f636b6574732f642f746f74616c2e737667)[![License](https://camo.githubusercontent.com/70aad0707edd8d7c00cca8ae5367a802f0b5f5e732d7e121351bf3a425b3ac86/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c797861722f776562736f636b6574732f6c6963656e73652e737667)](https://camo.githubusercontent.com/70aad0707edd8d7c00cca8ae5367a802f0b5f5e732d7e121351bf3a425b3ac86/68747470733a2f2f706f7365722e707567782e6f72672f6f6c6c797861722f776562736f636b6574732f6c6963656e73652e737667)

Simple and multifunctional PHP WebSocket server

[![ollyxar websockets](https://camo.githubusercontent.com/52fdeaf85fa8d502d4864de882c89ee792bbc45d9282e0c79cb924754837b1b8/68747470733a2f2f6f6c6c797861722e636f6d2f696d672f626c6f672f6f77732e706e67)](https://camo.githubusercontent.com/52fdeaf85fa8d502d4864de882c89ee792bbc45d9282e0c79cb924754837b1b8/68747470733a2f2f6f6c6c797861722e636f6d2f696d672f626c6f672f6f77732e706e67)

#### Live chat example

[](#live-chat-example)

[![chat](https://camo.githubusercontent.com/b87d9612560c68aa95a4115d09247c7e60ef4160e53b775f2d215fcd7cb3fdea/68747470733a2f2f692e696d6775722e636f6d2f374d394c6854442e6a7067)](https://camo.githubusercontent.com/b87d9612560c68aa95a4115d09247c7e60ef4160e53b775f2d215fcd7cb3fdea/68747470733a2f2f692e696d6775722e636f6d2f374d394c6854442e6a7067)

#### Performance

[](#performance)

[![ollyxar websockets performance](https://camo.githubusercontent.com/a658dc44a7bc4b2bed4b3739010e6006156a23af8fae3b6877af8bcb9b004ac3/68747470733a2f2f6f6c6c797861722e636f6d2f696d672f626c6f672f7773732e706e67)](https://camo.githubusercontent.com/a658dc44a7bc4b2bed4b3739010e6006156a23af8fae3b6877af8bcb9b004ac3/68747470733a2f2f6f6c6c797861722e636f6d2f696d672f626c6f672f7773732e706e67)

Installing WebSockets
---------------------

[](#installing-websockets)

The recommended way to install WebSockets is through [Composer](http://getcomposer.org).

```
# Install Composer
curl -sS https://getcomposer.org/installer | 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
-----------------------

[](#simple-websocket-server)

### MyHandler.php

[](#myhandlerphp)

```
use Generator;
use Ollyxar\LaravelAuth\FileAuth;
use Ollyxar\WebSockets\{
    Frame,
    Handler as BaseHandler,
    Dispatcher
};

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

[](#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;
    }
```

### ws-server.php

[](#ws-serverphp)

```
/**
 * Lets start our server
 */
(new Server('0.0.0.0', 2083, 6, true))
    ->setCert()
    ->setPassPhrase()
    ->setHandler(MyHandler::class)
    ->run();
```

### Nginx reverse proxy

[](#nginx-reverse-proxy)

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

### Realtime testing (logging)

[](#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.

```
Logger::enable();
```

[![output logging](https://camo.githubusercontent.com/a86b233a632097f906d9d8dce8d301c3c5e322f565fe4a08f20c4701d320edd1/68747470733a2f2f692e696d6775722e636f6d2f4861756b67624c2e6a7067)](https://camo.githubusercontent.com/a86b233a632097f906d9d8dce8d301c3c5e322f565fe4a08f20c4701d320edd1/68747470733a2f2f692e696d6775722e636f6d2f4861756b67624c2e6a7067)

### Communicate with server outside the wss protocol

[](#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);
socket_close($socket);
```

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community18

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 95.2% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~19 days

Recently: every ~4 days

Total

16

Last Release

2927d ago

Major Versions

1.2.4 → 2.0.02018-03-09

PHP version history (3 changes)1.0PHP ^7.0

1.0.1PHP &gt;=7.1

2.2.0PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/94a445da70d70ada49f927d76cb247d3df7a6b4cafc5bd78abcd559d73d76e7c?d=identicon)[alexslipknot](/maintainers/alexslipknot)

---

Top Contributors

[![alexslipknot](https://avatars.githubusercontent.com/u/8543759?v=4)](https://github.com/alexslipknot "alexslipknot (40 commits)")[![digitalhuman](https://avatars.githubusercontent.com/u/780848?v=4)](https://github.com/digitalhuman "digitalhuman (1 commits)")[![koss-shtukert](https://avatars.githubusercontent.com/u/709096?v=4)](https://github.com/koss-shtukert "koss-shtukert (1 commits)")

---

Tags

composerollyxar-websocketsphpphp-websocket-serverphp7php71unix-socketwebsocket-applicationwebsocket-librarywebsocket-serverwebsocketsphpframeworklibraryunixwebsocket serverweb-sockets

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ollyxar-websockets/health.svg)

```
[![Health](https://phpackages.com/badges/ollyxar-websockets/health.svg)](https://phpackages.com/packages/ollyxar-websockets)
```

###  Alternatives

[hprose/hprose

It is a modern, lightweight, cross-language, cross-platform, object-oriented, high performance, remote dynamic communication middleware. It is not only easy to use, but powerful. You just need a little time to learn, then you can use it to easily construct cross language cross platform distributed application system.

2.0k223.4k37](/packages/hprose-hprose)[digitalstars/simplevk

Powerful PHP library/framework for VK API bots, supporting LongPoll &amp; Callback &amp; OAuth

924.7k3](/packages/digitalstars-simplevk)[marwanalsoltany/mighty

The last validation library you will ever need!

601.3k](/packages/marwanalsoltany-mighty)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
