An asynchronous, event-driven TCP socket & HTTP device bridge library for PHP built on ReactPHP.
Run a non-blocking TCP device hub and an HTTP control/dashboard bridge on a single ReactPHP event loop — register TCP devices in memory, forward messages between devices, check device liveness, and push commands from HTTP APIs directly to active TCP sockets.
- PSR-4 Library Architecture (
Hotroso\ReactPhpDeviceBridge\): Zero global state, immutableConfig, injectableLoopInterfaceand PSR-3LoggerInterface. - Reliable Newline TCP Framing: Per-connection buffer handles fragmented or coalesced TCP packets cleanly, with configurable
maxLineBytesprotection. - Constant-Time Token Auth: Authenticates every TCP frame (
LOGIN,DATA,PING) withhash_equals(), plus optional Bearer/query token auth for HTTP endpoints. - Device Registry & Auto-Cleanup: Tracks connected devices in memory and automatically removes entries when sockets close.
- HTTP JSON API & Built-In Dashboard: Includes
GET /api/devices,POST /api/send, backward-compatibleGET /tcp/?id=...&msg=..., and an optional lightweight HTML dashboard (GET /). - Event Hooks: Subscribe to
login,data,ping, anddisconnectevents onTcpDeviceServer.
composer require hotroso/reactphp-device-bridgeRequirements: PHP >= 8.1 with ext-json.
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use Hotroso\ReactPhpDeviceBridge\Config;
use Hotroso\ReactPhpDeviceBridge\Device;
use Hotroso\ReactPhpDeviceBridge\DeviceBridge;
$bridge = new DeviceBridge(new Config(
authToken: 'your-secret-token',
socketUri: '0.0.0.0:6969',
httpUri: '0.0.0.0:6869',
httpAuthToken: null, // Optional Bearer token for HTTP endpoints
enableDashboard: true, // Serve built-in HTML dashboard at GET /
));
$bridge->tcpServer()->on('login', static function (Device $device): void {
echo "Device connected: {$device->id} ({$device->name})\n";
});
$bridge->run();See examples/server.php for a complete runnable script:
BRIDGE_AUTH_TOKEN=my-secret php examples/server.phpEvery TCP frame is newline-terminated (\n) and comma-delimited by default (<auth>,<command>,<device_id>[,...]).
| Command | Frame Format | Response |
|---|---|---|
| LOGIN | <auth>,LOGIN,<device_id>[,<device_name>]\n |
Login OK\n |
| DATA | <auth>,DATA,<from_id>,<target_id>,<payload...>\n |
Forwards <payload>\n to <target_id>, or replies die\n if offline |
| PING | <auth>,PING,<from_id>,<target_id>\n |
OK\n if alive, die\n if offline |
| Invalid auth | Any frame with mismatched token | AUTH_FAIL\n |
| Method & Path | Description |
|---|---|
GET / |
Built-in HTML dashboard listing connected devices with send forms (returns JSON if enableDashboard: false) |
GET /api/devices |
JSON list of live connected devices ({ ok, count, devices }) |
POST /api/send |
Push message to a device via JSON {"id":"dev01","msg":"hello"} or form/query params |
GET /tcp/?id=dev01&msg=hello |
Legacy plain-text push endpoint (backward-compatible) |
If httpAuthToken is configured in Config, include either Authorization: Bearer <token> or ?token=<token> on HTTP requests.
composer install
composer testReleased under the MIT License. A project by Hotroso.