# TecnoCric PHP Realtime (SSE) Setup

This build replaces the localStorage-only bridge with a PHP/MySQL event relay. The browser receives events using native `EventSource` (SSE) and publishes commands using HTTP POST. There is no WebSocket, Ratchet, Composer vendor, or browser localStorage dependency for realtime delivery.

## Files added/changed

- `api/overlay_realtime.php` — PHP publish endpoint + SSE stream endpoint. It creates `auction_realtime_events` automatically on first request.
- `assets/js/sse-client.js` — compatibility client used by the existing controller and overlay themes. Existing `new SSEClient(...)`, `.send(...)`, `.onmessage`, `.onopen`, and `.close()` calls remain supported.
- `pages/overlay/netlify/overlays/overlay_controller.php` — PHP version of the overlay controller. It validates the auction ID and loads the shared PHP realtime client.
- `pages/overlay/netlify/overlays/overlay_controller.html` — compatibility redirect to the PHP controller.

## Local XAMPP test

1. Extract the project under `C:\xampp\htdocs\tecnocric`.
2. Confirm root `.env` has the correct MySQL database credentials.
3. Start Apache and MySQL; ensure the auction exists in `auctions`.
4. Open `http://localhost/tecnocric/pages/overlay/netlify/overlays/overlay_controller.php?auction_id=1` (replace `1` with your auction ID).
5. Open a theme in another browser tab/window or on another device using its Netlify URL and the same `?auction_id=1`.
6. Use the overlay controller to show a view or send a stinger. Use the auction controller to broadcast a player/bid. The PHP API relays messages through MySQL, so same-origin localStorage is not involved.

## API

- Stream: `GET /api/overlay_realtime.php?mode=stream&auction_id=1`
- Publish: `POST /api/overlay_realtime.php?mode=publish&auction_id=1` with JSON body, e.g. `{"type":"PLAY_STINGER"}`.

The endpoint supports the existing `GET_*` requests and returns the corresponding data event types (`TEAMS_DATA`, `PURSE_TABLE`, `TEAM_PLAYERS`, `TOP_5_BUYS`, etc.) using the project database.

## Important deployment notes

- Deploy the PHP project, including `api/overlay_realtime.php` and `assets/js/sse-client.js`, to the PHP server. Static Netlify overlays load the shared JS client from the PHP server and subscribe to its SSE endpoint.
- Ensure HTTPS is enabled on the PHP domain. Netlify HTTPS pages cannot load an insecure HTTP SSE endpoint.
- PHP hosting must permit long-running streaming responses and flush output. The endpoint reconnects every 25 seconds; the browser's EventSource automatically resumes from the last event ID.
- `auction_realtime_events` is created automatically. Old rows older than 7 days are periodically cleaned up.
- The publish endpoint is currently CORS-enabled for the static overlay pages and does not implement user authentication. Before exposing it publicly for a production auction, protect publishing with the same authenticated admin/session or a server-side signed token. Do not put a permanent secret in public JavaScript.
- Verify the host's PHP worker limit with the expected number of connected overlays. Every connected SSE client occupies a PHP request while streaming.

## Syntax checks

Run `php -l api/overlay_realtime.php`, `php -l pages/overlay/netlify/overlays/overlay_controller.php`, and `node --check assets/js/sse-client.js` after deployment changes.
