Built-in HTTP Server
xs serve
includes an optional HTTP server that can expose your event stream
over HTTP/HTTPS. This is distinct from the store supervisor API (which uses
Unix domain sockets) and is meant for serving external web requests.
Starting the Server
Enable the HTTP server by passing the --http
flag with a port:
xs serve ./store --http :5007
How it Works
- When a request arrives, the server creates an
http.request
event with metadata about the request - You can respond by appending an
http.response
event referencing the request ID - The server processes the response and sends it back to the client
Request Events
Incoming requests generate events with this structure:
{ "topic": "http.request", "id": "03cp38qrua78v15fkmiaq6baz", "meta": { "method": "GET", "uri": "/", "headers": { "host": "localhost:5007" }, "query": {} }}
Response Format
To respond, append an event:
echo '<h1>Hello</h1>' | xs append ./store http.response \ --meta '{ "request_id": "03cp38qrua78v15fkmiaq6baz", "status": 200, "headers": { "Content-Type": "text/html" } }'
Response metadata fields:
request_id
: ID of the request event (required)status
: HTTP status code (default: 200)headers
: Response headersmore
: Enable streaming responses (default: false)
Streaming Responses
Use the more
flag to keep connections open for streaming:
# Initial response with headersecho 'data: event 1' | xs append ./store http.response \ --meta '{ "request_id": "03cp38qrua78v15fkmiaq6baz", "headers": {"Content-Type": "text/event-stream"}, "more": true }'
# Stream additional dataecho 'data: event 2' | xs append ./store http.response \ --meta '{ "request_id": "03cp38qrua78v15fkmiaq6baz", "more": true }'
# Close the streamxs append ./store http.response \ --meta '{ "request_id": "03cp38qrua78v15fkmiaq6baz", "more": false }'