PHPackages                             paragi/php-websocket-client - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. paragi/php-websocket-client

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

paragi/php-websocket-client
===========================

Websocket client

1.0.0(4y ago)14322.8k↑62.5%60[1 issues](https://github.com/paragi/PHP-websocket-client/issues)MITPHP

Since Mar 22Pushed 4y ago8 watchersCompare

[ Source](https://github.com/paragi/PHP-websocket-client)[ Packagist](https://packagist.org/packages/paragi/php-websocket-client)[ RSS](/packages/paragi-php-websocket-client/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Websocket client for PHP
------------------------

[](#websocket-client-for-php)

Use PHP to connect to at websocket service. These 3 methods make the websocket negotiation and connection and handle the hybi10 frame encoding required.

Note : unlike the library ratchetphp/Pawl, this a synchronous library that could be used within blocking kernels like Symfony for example.

Example 1:

```
try {
   $sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',80) );
   $sp->write("hello server");
   echo "Server responded with: " . $sp->read();
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
   echo "Something gets wrong ".$e->getMessage();
}
```

Example 2, using a session cookie and setting timeout:

```
try {
    $headers = ["Cookie: SID=".session_id()];
    $sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',80,$headers,$errstr,16);
    $bytes_written = $sp->write("hello server");
    $data = $sp->read();
    echo "Server responded with: ". $data;
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
   echo "Something gets wrong ".$e->getMessage();
}
```

Example 3, using SSL

```
try {
    $sp = new \Paragi\PhpWebsocket\Client('echo.websocket.org',443,'',$errstr, 10,true) ) {
    $sp->write("hello server");
    echo "Server responded with: " . $sp->read();
} catch (\Paragi\PhpWebsocket\ConnectionException $e) {
   echo "Something gets wrong ".$e->getMessage();
}
```

Client class
============

[](#client-class)

Constructor
-----------

[](#constructor)

Open websocket connection

\_\_construct(`string` $host \[, `int` $port \[, `array` $additional\_headers \[, `string` &amp;error\_string \[, `int` $timeout \[, `resource` $context\]\]\]\]\] )

**host** A host URL. It can be a domain name like [www.example.com](http://www.example.com) or an IP address like local host: 127.0.0.1

**port** The servers port number

**headers** (optional) additional HTTP headers to attach to the request. For example to parse a session cookie.

**error\_string** (optional) A referenced variable to store error messages, if any.

**timeout** (optional) The maximum time in seconds, a read operation will wait for an answer from the server. Default value is 10 seconds.

**context** (optional) A stream context resource created with stream\_context\_create() used to set various socket stream options.

write
-----

[](#write)

Send data to server through the websocket, using hybi10 frame encoding.

`int` write(`string` $data \[,`boolean` $final\])

**data** Data to transport to server

**final** (optional) indicate if this block is the final data block of this request. Default true

read
----

[](#read)

Read data through websocket from the server, using hybi10 frame encoding.

`string` read(\[`string` &amp;error\_string\])

**error\_string** (optional) A referenced variable to store error messages, i any.

Note:

- This implementation waits for the final chunk of data, before returning.
- Reading data while handling/ignoring other kind of packages

Exception
=========

[](#exception)

If anything goes wrong (connection, write, read...), a ConnectionException is thrown back. Please catch them all to return a comprehensive message (here's is a sample in a Symfony context) :

```
try {
    $sp = new \Paragi\PhpWebsocket\Client($this->localIp, $this->wsPort, ['X-Pusher: Symfony']);
    $sp->write($data);
    $reading = $sp->read();
    $ret = "Server responded with: $reading";

    return new JsonResponse(['level' => 'success', 'message' => $ret], Response::HTTP_OK);
} catch (ConnectionException $ex) {
    return new JsonResponse(['level' => 'error', 'message' => $ex->getMessage()], Response::HTTP_SERVICE_UNAVAILABLE);
}
```

Tests
=====

[](#tests)

Tests are running against a local echo server implemented with Ratchet. You'll have to manually start the server in ANOTHER process.

```
$ php tests/bin/echoserver.php
```

And now you can launch PhpUnit

```
$ vendor/bin/phpunit
```

Code coverage
=============

[](#code-coverage)

With PhpDbg, just launch :

```
$ phpdbg -qrr vendor/bin/phpunit
$ firefox .coverage/index.html
```

Contribute
==========

[](#contribute)

Please let me know if there is any problems with the code. Any contributions are accepted, if the code looks nice, not bloated and otherwise reasonable.

License: MIT: Free
==================

[](#license-mit-free)

TODO
====

[](#todo)

- Achieving 100% Code Coverage

###  Health Score

37

—

LowBetter than 83% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity47

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

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

Unknown

Total

1

Last Release

1518d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2a6095dc96c4ceea5a3de9b71d99834f1057f510a15b2cf6d7ec563138ef9fab?d=identicon)[paragi](/maintainers/paragi)

---

Top Contributors

[![paragi](https://avatars.githubusercontent.com/u/4333973?v=4)](https://github.com/paragi "paragi (34 commits)")[![Trismegiste](https://avatars.githubusercontent.com/u/1260026?v=4)](https://github.com/Trismegiste "Trismegiste (14 commits)")[![NabiKAZ](https://avatars.githubusercontent.com/u/246721?v=4)](https://github.com/NabiKAZ "NabiKAZ (3 commits)")[![cpinkham](https://avatars.githubusercontent.com/u/475112?v=4)](https://github.com/cpinkham "cpinkham (1 commits)")[![tsukasagenesis](https://avatars.githubusercontent.com/u/374009?v=4)](https://github.com/tsukasagenesis "tsukasagenesis (1 commits)")[![Valkinaz](https://avatars.githubusercontent.com/u/24223502?v=4)](https://github.com/Valkinaz "Valkinaz (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/paragi-php-websocket-client/health.svg)

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

PHPackages © 2026

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