Skip to content
sinhsacPublic

About

Asynchronous event-driven TCP socket and HTTP bridge server built with PHP and ReactPHP for managing connected devices and real-time message forwarding.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

Repository files navigation

ReactPHP TCP Socket & HTTP Device Server

License: MIT PHP ReactPHP

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/http 0.8) is archived under release v0.1.0-legacy.

Key Features

  • 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 processing LOGIN, DATA, and PING commands.
  • 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.

Tech Stack & Project Structure

  • 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

Prerequisites

  • PHP >= 8.1 with the json extension enabled
  • Composer 2.x

Getting Started

  1. Install dependencies in the project directory:

    composer install
  2. Configure server settings in config.php:

    Adjust HTTP_PORT (default 6869), SOCKET_PORT (default 6969), and AUTH_STR (shared authentication token for TCP clients) as needed:

    define('AUTH_STR', 'your_secret_token');
    define('HTTP_PORT', '6869');
    define('SOCKET_PORT', '6969');
  3. 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:6869 and the TCP socket server listens on 0.0.0.0:6969 (using the ports configured in config.php).

Protocol & Usage

TCP Socket Protocol

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.

HTTP Interface

  • 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 device01 is connected, hello\n is written to its TCP socket and the HTTP endpoint responds with Send data success to device01 with msg hello.

License

This project is licensed under the MIT License — see the LICENSE file for details.

About

Asynchronous event-driven TCP socket and HTTP bridge server built with PHP and ReactPHP for managing connected devices and real-time message forwarding.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages