PHPackages                             xp-forge/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. xp-forge/websockets

ActiveLibrary[Framework](/categories/framework)

xp-forge/websockets
===================

WebSockets for the XP Framework

v4.3.0(3w ago)03.1k↓71.4%[1 issues](https://github.com/xp-forge/websockets/issues)1BSD-3-ClausePHPPHP &gt;=7.4.0CI passing

Since Sep 23Pushed 3w ago1 watchersCompare

[ Source](https://github.com/xp-forge/websockets)[ Packagist](https://packagist.org/packages/xp-forge/websockets)[ Docs](http://xp-framework.net/)[ RSS](/packages/xp-forge-websockets/feed)WikiDiscussions master Synced 1w ago

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

WebSockets for the XP Framework
===============================

[](#websockets-for-the-xp-framework)

[![Build status on GitHub](https://github.com/xp-forge/websockets/workflows/Tests/badge.svg)](https://github.com/xp-forge/websockets/actions)[![XP Framework Module](https://raw.githubusercontent.com/xp-framework/web/master/static/xp-framework-badge.png)](https://github.com/xp-framework/core)[![BSD Licence](https://raw.githubusercontent.com/xp-framework/web/master/static/licence-bsd.png)](https://github.com/xp-framework/core/blob/master/LICENCE.md)[![Requires PHP 7.4+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-7_4plus.svg)](http://php.net/)[![Supports PHP 8.0+](https://raw.githubusercontent.com/xp-framework/web/master/static/php-8_0plus.svg)](http://php.net/)[![Latest Stable Version](https://camo.githubusercontent.com/b4f9e42f5fc75e30cbe08b12bea985df1e89cd8acb9b924e190a8d7c2fa5342f/68747470733a2f2f706f7365722e707567782e6f72672f78702d666f7267652f776562736f636b6574732f76657273696f6e2e737667)](https://packagist.org/packages/xp-forge/websockets)

Example
-------

[](#example)

```
use websocket\Listeners;

class Imitator extends Listeners {

  public function serve($events) {
    return [
      '/echo' => function($conn, $payload) {
        $conn->send('You said: '.$payload);
      }
    ];
  }
}
```

Run it using:

```
$ xp -supervise ws Imitator
@peer.ServerSocket(Resource id #138 -> tcp://0.0.0.0:8081))
Serving Imitator(dev)[] > websocket.logging.ToConsole
# ...
```

To connect to this server, use the following:

```
use util\cmd\Console;
use websocket\WebSocket;

$s= new WebSocket('ws://localhost:8081/echo');
$s->connect();

$s->send('Hello');
Console::writeLine('
