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

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

dimogrudev/php-websocket
========================

No-dependency implementation of WebSocket server in PHP

v2.0.0(3w ago)013MITPHPCI passing

Since Jul 19Pushed 3w ago1 watchersCompare

[ Source](https://github.com/dimogrudev/php-websocket)[ Packagist](https://packagist.org/packages/dimogrudev/php-websocket)[ RSS](/packages/dimogrudev-php-websocket/feed)WikiDiscussions main Synced today

READMEChangelog (2)Dependencies (2)Versions (3)Used By (0)

PHP WebSocket Server
====================

[](#php-websocket-server)

A zero-dependency native implementation of WebSocket server in PHP.

Lightweight and minimalistic.

Features
--------

[](#features)

- Strict adherence to [RFC 6455](https://datatracker.ietf.org/doc/html/rfc6455)
- Automatic HTTP Upgrade handshake compliant with [RFC 9110](https://datatracker.ietf.org/doc/html/rfc9110) and [RFC 9112](https://datatracker.ietf.org/doc/html/rfc9112)
- SSL/TLS encryption support
- Binary and textual data message support, both sending and receiving
- Transparent reassembly of incoming fragmented messages
- Built-in handling for Ping, Pong, and Close control frames
- User-defined non-blocking timers
- Non-blocking I/O

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

[](#requirements)

- PHP 8.4 or higher (64-bit)

Important

If you plan to run websockets on a shared hosting, note that most providers block ports for any third-party usage. A VPS or dedicated server is highly recommended.

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

[](#installation)

This library may be installed via [Composer](https://getcomposer.org/):

```
composer require dimogrudev/php-websocket
```

Usage
-----

[](#usage)

```
use WebSocket\Server;
use WebSocket\Contract\ClientInterface;
use WebSocket\Contract\RequestInterface;
use WebSocket\Contract\MessageInterface;

require 'vendor/autoload.php';

// Create an instance of the server class
// 0.0.0.0 is set as host to make the server reachable at all IPv4 addresses
$server = new Server('0.0.0.0', 8443);
// Enable encryption and provide certificate files
$server->encryption(true, 'path/to/cert.crt', 'path/to/cert.key');

// Handle incoming messages and respond to clients
$server->onMessageReceive(function (ClientInterface $client, MessageInterface $message): void {
    if ($message->isBinary) {
        echo "{$client->ipAddr} (#{$client->id}) sends binary message ({$message->length} bytes)\n";

        // Echo the binary message back to the client
        $client->send($message->payload, isBinary: true);
    } else {
        echo "{$client->ipAddr} (#{$client->id}) sends `{$message->payload}`\n";

        // Reply with a text message
        $client->send("Server received your message: {$message->payload}");
    }
});

$server->start();
```

Tip

Modern browsers block non-secure WebSocket connections (`ws://`) on secure websites (`https://`). Always use a valid SSL/TLS certificate (e.g. **Let's Encrypt**) for production.

### Timers

[](#timers)

```
// Create a timer to run function repeatedly with a 500 milliseconds interval
// It provides timer ID which may be used for later cancellation
$timerId = $server->setTimer(function (): void {
    // Your logic here
}, 500, true);

// Cancel the timer
$server->clearTimer($timerId);
```

### Callbacks Reference

[](#callbacks-reference)

MethodSignatureDescription`onServerStart``(): void`Triggered when the server starts listening.`onServerStop``(): void`Triggered when the server stops.`onHandshake``(ClientInterface, RequestInterface): bool`Triggered when a handshake request is received. Return `true` to accept the connection.`onClientConnect``(ClientInterface): void`Triggered after the handshake is accepted and the connection is fully established.`onClientDisconnect``(ClientInterface): void`Triggered when the connection is closed.`onMessageReceive``(ClientInterface, MessageInterface): void`Triggered when a complete data message is received.### Connection Security

[](#connection-security)

Unauthorized connections may be rejected by using the `onHandshake` callback:

```
$server->onHandshake(function (ClientInterface $client, RequestInterface $request): bool {
    // Reject connection if it's not from your trusted domain
    if ($request->header('origin') !== 'https://example.com') {
        return false;
    }
    return true;
});
```

License
-------

[](#license)

The MIT License (MIT). For more information, please see [LICENSE](/LICENSE).

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance95

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 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.

###  Release Activity

Cadence

Every ~322 days

Total

2

Last Release

27d ago

Major Versions

v1.0.0 → v2.0.02026-06-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/385921417b0621b693ff4fe0ad6ce11869af03e860f929697953b7a67f3e3a8a?d=identicon)[dimogrudev](/maintainers/dimogrudev)

---

Top Contributors

[![dimogrudev](https://avatars.githubusercontent.com/u/95988098?v=4)](https://github.com/dimogrudev "dimogrudev (93 commits)")

---

Tags

dddevent-looplibrarylightweightminimalphpphp8rfc-6455serverwebsocketserverwebsocketrfc6455

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[cboden/ratchet

PHP WebSocket library

6.4k22.3M254](/packages/cboden-ratchet)[hhxsv5/laravel-s

🚀 LaravelS is an out-of-the-box adapter between Laravel/Lumen and Swoole.

3.9k691.5k13](/packages/hhxsv5-laravel-s)[swow/swow

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

1.3k2.2M89](/packages/swow-swow)[ratchet/rfc6455

RFC6455 WebSocket protocol handler

23641.1M76](/packages/ratchet-rfc6455)[phrity/websocket

WebSocket client and server

2164.9M41](/packages/phrity-websocket)[amphp/websocket-server

Websocket server for Amp's HTTP server.

125282.8k30](/packages/amphp-websocket-server)

PHPackages © 2026

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