An asynchronous, event-driven TCP socket and HTTP bridge server built with PHP and ReactPHP. Running both servers on a single non-blocking event loop, it registers connected TCP devices in memory, forwards messages between devices, checks device liveness, and exposes a lightweight HTTP dashboard and query interface to push commands to active connections.
Note: The original legacy version (PHP 7.x /
react/http0.8) is archived under releasev0.1.0-legacy.
- Unified ReactPHP Event Loop: Runs both a TCP socket server (
react/socket) and an HTTP server (react/http) concurrently in a single PHP process (main.php). - Authenticated TCP Device Protocol: Validates incoming comma-delimited frames against a configurable shared token (
AUTH_STR) before processingLOGIN,DATA, andPINGcommands. - Device-to-Device Message Forwarding: Routes payloads between registered TCP devices by device ID and automatically detects stale or disconnected sockets.
- HTTP Dashboard & Push Endpoint: Lists currently connected devices (ID, name, and TCP/IP remote address) in a Bootstrap HTML view and lets you send text messages directly to any connected device via HTTP query parameters.
- Zero External Database Required: Tracks active device connections, names, and login timestamps in memory with automatic cleanup of closed connections.
- Language: PHP 8.1+ (
ext-json) - Libraries:
react/socket(^1.16),react/http(^1.11.1) - Package Manager: Composer
.
├── main.php # Entry point: initializes ReactPHP event loop, HTTP server, and TCP server
├── config.php # Port configuration (HTTP_PORT, SOCKET_PORT), auth token, and key constants
├── composer.json # Composer dependencies (react/socket, react/http, ext-json)
├── modules/
│ ├── HttpServer.php # ReactPHP HTTP server for device listing UI and HTTP-to-TCP message dispatch
│ └── SocketServer.php # ReactPHP TCP socket server parsing LOGIN, DATA, and PING frames
├── handler/
│ └── SocketHandler.php # Connection registration, device liveness checks, and message forwarding
├── helper/
│ ├── CollectionUtils.php # Array length validation helper
│ └── DataUtils.php # Payload emptiness validation helper
└── libs/
├── Page.php # Base JSON response helper class
├── DevicePage.php # JSON API handler for listing devices and posting messages
└── func.php # Placeholder home handler helper
- PHP
>= 8.1with thejsonextension enabled - Composer 2.x
-
Install dependencies in the project directory:
composer install
-
Configure server settings in
config.php:Adjust
HTTP_PORT(default6869),SOCKET_PORT(default6969), andAUTH_STR(shared authentication token for TCP clients) as needed:define('AUTH_STR', 'your_secret_token'); define('HTTP_PORT', '6869'); define('SOCKET_PORT', '6969');
-
Start the server:
php main.php
Or run it in the background as a daemon:
nohup php main.php &Once started, the HTTP server listens on
http://0.0.0.0:6869and the TCP socket server listens on0.0.0.0:6969(using the ports configured inconfig.php).
Connect to <host>:<SOCKET_PORT> over TCP and send comma-separated frames prefixed with your configured AUTH_STR:
| Command | Frame Format | Behavior & Response |
|---|---|---|
LOGIN |
<AUTH_STR>,LOGIN,<DEVICE_ID>,<DEVICE_NAME> |
Registers the device connection in memory and responds with Login OK. |
DATA |
<AUTH_STR>,DATA,<SOURCE_ID>,<DEST_ID>,<PAYLOAD...> |
Forwards <PAYLOAD...> (comma-joined) to <DEST_ID>. Responds with die if the target device is offline. |
PING |
<AUTH_STR>,PING,<SOURCE_ID>,<DEST_ID> |
Checks whether <DEST_ID> is connected. Responds with OK if alive or die otherwise. |
-
View Connected Devices: Open
http://localhost:6869/in a browser to see active devices and send messages via the inline form. -
Send a Message via HTTP Query:
curl "http://localhost:6869/?id=device01&msg=hello"If
device01is connected,hello\nis written to its TCP socket and the HTTP endpoint responds withSend data success to device01 with msg hello.
This project is licensed under the MIT License — see the LICENSE file for details.