Skip to content

Implement File-Based UHST Relay Server in PHP #1

@dimitrovs

Description

@dimitrovs

Goal
Implement a PHP version of the UHST (User Hosted Session Traversal) relay server. The implementation must be lightweight, dependency-free, and use local files for coordination, making it suitable for
"drop-in" deployment on shared hosting providers.

Core Requirements

  • No Dependencies: Do not use Composer or any external libraries. Use standard PHP (7.4+).
  • File-based Coordination: Use local files in a data/ directory to store session and message information.
  • Concurrency Safety: Use flock() to ensure safe access to data files from multiple concurrent PHP processes.
  • SSE Support: Implement a reliable Server-Sent Events (SSE) stream for message delivery.

Protocol Specification

  1. Register Host
  • Endpoint: POST /?action=host
  • Response: 200 OK with JSON: {"hostId": "...", "hostToken": "..."}
  • Behavior: Create a new host session with a unique ID and a private token.
  1. Join Host
  • Endpoint: POST /?action=join&hostId=
  • Response: 200 OK with JSON: {"clientToken": "..."}
  • Behavior: Associate a new client with the specified host and return a unique client token.
  1. SSE Connection
  • Endpoint: GET /?token=
  • Headers:
    • Content-Type: text/event-stream
    • Cache-Control: no-cache
    • Connection: keep-alive
  • Behavior:
    • Immediately send : Connected. followed by a newline.
    • Send : sse-keep-alive every 5 seconds to keep the connection open.
    • When a message is sent to this token's peer, deliver it via the SSE stream:
      • From Client to Host: data: {"body": , "responseToken": "<temp_token_for_reply>"}
      • From Host to Client: data: {"body": }
    • The responseToken should allow the host to send a message back to that specific client.
  1. Send Message
  • Endpoint: POST /?token=
  • Body: JSON payload.
  • Behavior:
    • If token is a clientToken, queue the message for the host's SSE.
    • If token is a responseToken, queue the message for that specific client's SSE.
  1. Ping
  • Endpoint: POST /?action=ping&timestamp=
  • Response: 200 OK.

Implementation Hints

  • Responsive SSE: Use a small usleep() in the SSE loop (e.g., 10-50ms) when checking for new messages to ensure low latency for the performance tests.
  • Cleanup: Periodically delete session files that haven't been accessed for more than 60 seconds.
  • Output Buffering: Call ob_flush() and flush() after every SSE message to ensure it is sent immediately.

Verification
The implementation must pass the existing Python performance tests.

  1. Start the PHP server: php -S localhost:8080 index.php
  2. Run the performance test suite (git@github.com:uhst/performance-test.git):

1 # Create a temporary relay config
2 echo '[{"urls": ["http://localhost:8080"], "prefix": "local-php"}]' > local_relay.json
3 # Run all tests
4 python3 perftest.py --relays local_relay.json
3. Ensure all tests (ping, register, roundtrip, throughput) pass without errors and with reasonable latency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions