PHPackages                             hemiframe/php-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. hemiframe/php-websocket

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

hemiframe/php-websocket
=======================

PHP WebSocket server and client library

1.7.1(7y ago)4911.0k—0%13[4 issues](https://github.com/heminei/php-websocket/issues)MITPHPPHP &gt;=7.0

Since Dec 24Pushed 7y ago4 watchersCompare

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

READMEChangelog (6)DependenciesVersions (6)Used By (0)

Simple and Powerfull PHP WebSocket Server and Client
====================================================

[](#simple-and-powerfull-php-websocket-server-and-client)

Simple and powerful PHP WebSocket implementation for PHP

Supports
--------

[](#supports)

- multiple paths
- check-origin
- limit clients
- sends status codes on close connections
- sends custom headers

Requirements
------------

[](#requirements)

PHP &gt;= 7.0

Installation
------------

[](#installation)

Enter in composer.json

```
{
    "require": {
        "hemiframe/php-websocket": "~1.7"
    }
}
```

or run in console: `php composer.phar require hemiframe/php-websocket`

How to run demo?
----------------

[](#how-to-run-demo)

Open CMD or terminal and enter

```
php demo/server.php

```

After that you can open demo/index.html in your browser

Server example
--------------

[](#server-example)

```
require_once('vendor/autoload.php');

$socket = new \HemiFrame\Lib\WebSocket\WebSocket("localhost", 8080);
$socket->on("receive", function($client, $data) use($socket) {
	foreach ($socket->getClients() as $item) {
        /* @var $item \HemiFrame\Lib\WebSocket\Client */
		if ($item->id != $client->id) {
			$socket->sendData($item, $data);
		}
	}
});
$socket->startServer();
```

Client example
--------------

[](#client-example)

```
require_once('vendor/autoload.php');

$socket = new \HemiFrame\Lib\WebSocket\WebSocket('localhost', 8080);
$socket->on("receive", function($client, $data) use($socket) {
});
$client = $socket->connect();
if ($client) {
	$socket->sendData($client, "Message");
	$socket->disconnectClient($client);
}
```

Documentation
-------------

[](#documentation)

### Events:

[](#events)

```
$socket->on("connect", function(\HemiFrame\Lib\WebSocket\Client $client) {
});

$socket->on("receive", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("send", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("ping", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("pong", function(\HemiFrame\Lib\WebSocket\Client $client, $data) {
});

$socket->on("disconnect", function(\HemiFrame\Lib\WebSocket\Client $client, $statusCode, $reason) {
});

$socket->on("error", function($socket, $client, $phpError, $errorMessage, $errorCode) {
});
```

### Methods

[](#methods)

```
$socket->getSocket();

/**
* Get connected clients
* @return array
*/
$socket->getClients();

/**
* Get connected clients, filtered by path
* @return array
*/
$socket->getClientsByPath($path = "/");

$socket->getAllowedOrigins();

$socket->setAllowedOrigins($allowedOrigins);

$socket->getMaxClients();

$socket->setMaxClients($maxClients);

$socket->getBufferSize();

$socket->setBufferSize($bufferSize);

$socket->getUserAgent();

$socket->setUserAgent($userAgent);

$socket->setEnableLogging($enableLogging);

$socket->create();

$socket->close();

$socket->setOption($socket, $level, $optname, $optval);

$socket->bind($address, $port = 0);

$socket->listen($backlog = 0);

$socket->select(&$read, &$write, &$except, $backlog, $tv_usec = 0);

$socket->accept();

$socket->write($socket, $message = null);

$socket->recv($socket);

$socket->read($socket);

/**
* Get last socket error code
* Alias of socket_last_error
* @return int
*/
$socket->getLastErrorCode();

/**
* Get last socket error message
* @return string
*/
$socket->getLastErrorMessage();

/**
* Connect to websocket server. Send and validate handshake request.
* @param string $path Send custom path to server
* @param string $origin Send custom origin host to server
* @return mixed Return client object when is successfully connected to server or FALSE when is not.
*/
$socket->connect($path = "/", $origin = null);

/**
* Run WebSocket server.
* Create main socket.
* Bind address and port.
* Listens for a connection on a socket.
* Call main processing loop
*/
$socket->startServer();

/**
* Disconnect all clients and close main socket
*/
$socket->stopServer();

/**
* Main processing loop. Check for new clients, incomming data, diconnected clients
*/
$socket->loop();

/**
* Trigger event
* @param string $name Event name
* @param array $arguments Arguments for sending
*/
$socket->trigger($name, $arguments);

/**
* Subscribe for event.
* @param string $name Event name
* @param function $function Callback function
*/
$socket->on($name, $function);

/**
* Subscribe on event. Call only first time
* @param string $name Event name
* @param function $function Callback function
*/
$socket->one($name, $function);

/**
* Send data to client
* @param object $client
* @param string $data
*/
$socket->sendData($client, $data);

/**
* Disconnect client. You can send custom status code and reason
* @param object $client
* @param int $statusCode
* @param string $reason
* @return self
*/
$socket->disconnectClient($client, $statusCode = self::STATUS_CLOSE_NORMAL, $reason = null);
```

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance18

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 94.4% 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 ~102 days

Total

5

Last Release

2658d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3735861?v=4)[Martin Bratvanov](/maintainers/heminei)[@heminei](https://github.com/heminei)

---

Top Contributors

[![heminei](https://avatars.githubusercontent.com/u/3735861?v=4)](https://github.com/heminei "heminei (17 commits)")[![brainexe](https://avatars.githubusercontent.com/u/1894271?v=4)](https://github.com/brainexe "brainexe (1 commits)")

---

Tags

phpclientserverwebsocketWebSockets

### Embed Badge

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

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

###  Alternatives

[cboden/ratchet

PHP WebSocket library

6.4k21.4M239](/packages/cboden-ratchet)[swow/swow

Coroutine-based multi-platform support engine with a focus on concurrent I/O

1.3k2.1M84](/packages/swow-swow)[phrity/websocket

WebSocket client and server

2184.1M27](/packages/phrity-websocket)[datto/json-rpc-http

HTTP client and server for JSON-RPC 2.0

66525.9k5](/packages/datto-json-rpc-http)[warriorxk/phpwebsockets

A websocket library with support for IPC using socket pairs

1225.3k](/packages/warriorxk-phpwebsockets)

PHPackages © 2026

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