PHPackages                             t4web/websocket - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. t4web/websocket

ActiveLibrary[HTTP &amp; Networking](/categories/http)

t4web/websocket
===============

ZF2 Module. Websocket server

0221PHP

Since Jul 26Pushed 10y ago3 watchersCompare

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

READMEChangelogDependenciesVersions (1)Used By (0)

Websocket
=========

[](#websocket)

ZF2 Module. Websocket server build on [Ratchet](https://github.com/ratchetphp/Ratchet)

### Instalation

[](#instalation)

Just `require t4web/websocket` and add to your `application.config.php` `T4web\Websocket`.

### Run

[](#run)

In console run

```
$ php public/index.php websocket start
```

### Configuration

[](#configuration)

Add in your `global.config.php` :

```
't4web-websocket' => [
    'server' => [
        // required
        'port' => 8088,

        // not required, default 0
        'debug-enable' => 1,
    ],
];
```

### Quick usage

[](#quick-usage)

For testing create html (or copy from ):

```

            var host   = 'ws://127.0.0.1:8088';
            var socket = null;
            var input  = document.getElementById('input');
            var output = document.getElementById('output');
            var print  = function (message) {
                var samp       = document.createElement('samp');
                samp.innerHTML = message + '\n';
                output.appendChild(samp);
                return;
            };

            input.addEventListener('keyup', function (evt) {
                if (13 !== evt.keyCode) {
                    return;
                }

                var msg = {
                    'event' : 'ping',
                    'data': {message: input.value}
                };

                if (!msg) {
                    return;
                }

                try {
                    print('Send: ' + JSON.stringify(msg, null, 4));
                    socket.send(JSON.stringify(msg));
                    input.value = '';
                    input.focus();
                } catch (e) {
                    console.log(e);
                }

                return;
            });

            try {
                socket = new WebSocket(host);
                socket.onopen = function () {
                    print('connection is opened');
                    input.focus();
                    return;
                };
                socket.onmessage = function (msg) {
                    print('Receive: ' + JSON.stringify(JSON.parse(msg.data), null, 4));
                    return;
                };
                socket.onclose = function () {
                    print('connection is closed');
                    return;
                };
            } catch (e) {
                console.log(e);
            }

```

Or from PHP:

```
/** @var \T4web\Websocket\WebsocketClient $wsClient */
$wsClient = $this->getServiceLocator()->get(\T4web\Websocket\Client::class);
$wsClient->send('ping', ['message' => 'Hello World']);
```

### Proposed architecture

[](#proposed-architecture)

Each websocket request message must be json object:

```
{
    "event": "event-name",
    "data": { "field": "value" }
}
```

Websocket response message:

```
{
    "event": "event-name",
    "data": { "field": "value" },
    "error": "string|null"
}
```

Each websocket request mesage - you can handle. Just describe own handlers in `t4web-websocket[event-handlers]` `module.config.php`:

```
't4web-websocket' => [
    'event-handlers' => [
        // event name => handler - Callable object
        'ping' => Handler\Ping::class,
    ],
],
```

`Handler\Ping` :

```
use SplObjectStorage;
use Ratchet\ConnectionInterface;

class Ping implements T4web\Websocket\Handler\HandlerInterface
{
    public function handle($eventName, array $data, ConnectionInterface $connection, SplObjectStorage $connections)
    {
        $response = [
            'event' => 'pong',
            'data' => $data,
            'error' => null,
        ];
        $connection->send(json_encode($response));
    }
}
```

###  Health Score

21

—

LowBetter than 17% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/208688?v=4)[max](/maintainers/maxgu)[@maxgu](https://github.com/maxgu)

---

Top Contributors

[![maxgu](https://avatars.githubusercontent.com/u/208688?v=4)](https://github.com/maxgu "maxgu (17 commits)")

### Embed Badge

![Health badge](/badges/t4web-websocket/health.svg)

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

###  Alternatives

[php-http/cache-plugin

PSR-6 Cache plugin for HTTPlug

25026.8M85](/packages/php-http-cache-plugin)[httpsoft/http-message

Strict and fast implementation of PSR-7 and PSR-17

85997.3k126](/packages/httpsoft-http-message)[thesis/nats

Async (fiber based) client for Nats.

758.3k](/packages/thesis-nats)

PHPackages © 2026

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