Skip to content

About

Asynchronous, event-driven TCP socket and HTTP device bridge library for PHP built on ReactPHP — PSR-4, newline framing, token auth, HTTP API & dashboard.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

5 Commits

Folders and files

Repository files navigation

ReactPHP Device Bridge

CI Packagist Version PHP License: MIT

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, immutable Config, injectable LoopInterface and PSR-3 LoggerInterface.
  • Reliable Newline TCP Framing: Per-connection buffer handles fragmented or coalesced TCP packets cleanly, with configurable maxLineBytes protection.
  • Constant-Time Token Auth: Authenticates every TCP frame (LOGIN, DATA, PING) with hash_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-compatible GET /tcp/?id=...&msg=..., and an optional lightweight HTML dashboard (GET /).
  • Event Hooks: Subscribe to login, data, ping, and disconnect events on TcpDeviceServer.

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.

About

Asynchronous, event-driven TCP socket and HTTP device bridge library for PHP built on ReactPHP — PSR-4, newline framing, token auth, HTTP API & dashboard.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages