An HTTP and WebSocket server for the event-driven runtime. serve_http() starts the server, and every
request reaches PHP as a Baremetal\Http\Request event that a listener answers with a
Baremetal\Http\Response. serve_ws() adds a WebSocket endpoint whose inbound frames arrive as
Baremetal\Http\Message events; Message::reply() and ws_broadcast() send frames back. The script stays
resident, so ordinary PHP variables keep their state between requests.
ext-web is platform-neutral: it requires the virtual php-baremetal/web-hal and ext-events, and conductor
adds the HAL for your SDK platform (php-baremetal/esp32-web-hal on ESP32). It needs a network interface:
the network option on a wired board, or ext-wifi on a Wi-Fi board.
Install
In a project created with phpbm init, with the event-driven runtime:
composer require php-baremetal/ext-web
On a Wi-Fi board also require php-baremetal/ext-wifi and connect (or start an access point) before
serve_http().
Example
<?php
use Baremetal\Events;
use Baremetal\Http\Request;
use Baremetal\Http\Response;
$hits = 0;
Events::listen(Request::class, function (Request $r) use (&$hits): Response {
return match ($r->path) {
'/' => Response::html('<h1>hello from PHP</h1>'),
'/hits' => Response::json(['hits' => ++$hits]),
default => Response::notFound(),
};
});
serve_http(80);
Documentation
- Overview: requirements, install, a first project, notes.
- HTTP:
serve_http(),Request,Response. - WebSocket:
serve_ws(),Message,ws_broadcast(). - Request lifecycle and timeouts: status codes, superglobals,
request-timeout, 503 on stop. - Errors and limits
- Recipes: a JSON API, a page over Wi-Fi, WebSocket broadcast.
License
MIT.