hotroso / reactphp-device-bridge
Asynchronous, event-driven TCP socket and HTTP device bridge library built on ReactPHP for managing connected devices, forwarding messages, and pushing commands over HTTP.
Requires
- php: ^8.1
- ext-json: *
- psr/http-message: ^1.1 || ^2.0
- psr/log: ^2.0 || ^3.0
- react/event-loop: ^1.5
- react/http: ^1.11
- react/socket: ^1.16
Requires (Dev)
- phpunit/phpunit: ^10.5 || ^11.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
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.
Key Features
- 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.
Installation
composer require hotroso/reactphp-device-bridge
Requirements: PHP >= 8.1 with ext-json.
Quick Start
<?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.php
TCP Device Protocol
Every 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 |
HTTP Bridge Endpoints
| 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.
Testing
composer install
composer test
License
Released under the MIT License. A project by Hotroso.