Deploy FastAPI SSE and verify the response is not buffered
Deploy a streaming FastAPI endpoint on Gregale and measure when complete server-sent events arrive at the public URL.
A FastAPI endpoint is streaming in production when the client receives complete events while the response is still open. A 200 or a text/event-stream header does not prove it: a reverse proxy can hold the body and send all the events at once. On Gregale, enable response streaming for the app, then test the exact hostname your client will use.
Download the runnable FastAPI starter. It sends three synthetic events, one second apart. There is no model call, database, or provider API key here, so the test isolates delivery rather than application latency.
1. Yield complete SSE events
FastAPI's StreamingResponse sends chunks from an iterator. An SSE event ends with a blank line; without it a client can have received bytes but still have no complete event.
import asyncio
from fastapi import FastAPI
from fastapi.responses import StreamingResponse
app = FastAPI()
@app.get("/events")
async def events():
async def body():
for number in range(1, 4):
if number > 1:
await asyncio.sleep(1)
yield f"id: {number}\nevent: tick\ndata: {number}\n\n"
return StreamingResponse(
body(),
media_type="text/event-stream",
headers={"Cache-Control": "no-store, no-transform"},
)
The starter includes a container listening on the platform's port and a separate GET /health route. The cache header asks intermediaries not to store or transform the response; it does not by itself turn off proxy buffering.
2. Deploy with response streaming enabled
Install the Gregale CLI. The starter needs Bash, Python 3, curl (7.82+), jq and openssl. From the extracted directory, export a Gregale API key for a disposable workspace as GREGALE_TOKEN. It needs app creation, settings, deployment and cleanup access. The script reserves a new app, intentionally makes the synthetic endpoint public, deploys the container and checks the returned app URL. It tries to delete only the newly reserved app after the run and reports if cleanup could not be verified.
bash run-demo.sh
The setting that makes this different from a buffered deploy is:
gregale app update <test-app> --streaming-enabled
Gregale checks plan eligibility when enabling streaming. If the API rejects the setting, stop there: a successful ordinary deploy does not establish that streaming is enabled. Don't turn off authentication on an existing app to follow this example.
3. Measure the public response, not the server log
To run the same probe against a live copy of this endpoint:
python3 check-stream.py https://<test-app>.gregale.app
The probe uses the client's monotonic clock. It prints each event as it reads the terminating blank line, then prints when the response finishes. It checks all three IDs in order, rejects compressed responses in this baseline, and fails if consecutive events are less than 0.55 seconds apart or the first event is less than 1.25 seconds ahead of completion. Those thresholds allow timing jitter for this fixture; they are not latency targets for your app.
Illustrative passing output:
event 1 at +0.030s
event 2 at +1.031s
event 3 at +2.032s
response finished at +2.033s
PASS: all three events arrived separately and event 1 arrived before EOF.
For a quick look without the assertion, run:
curl -N -H 'Accept: text/event-stream' https://<test-app>.gregale.app/events
curl -N turns off curl's output buffering. It does not change what the server, a CDN or a proxy sends.
If all events show up at the end
| What you observe | What to check next |
|---|---|
| Events arrive together locally | Check that the generator yields each event as it becomes available, that each ends with a blank line and that middleware is not collecting the full body. |
| Local works, but the Gregale app hostname fails | Check that response streaming was accepted and that you're probing the deployed /events route, not a redirect, health check or an old release. |
| The app hostname works, but your public hostname fails | Repeat the probe through your actual reverse proxy or CDN. Compare compression and buffering settings on the streaming route. |
| Header arrives quickly, but the events arrive together | Measure the complete SSE frames, not just time to response headers or first byte. |
A pass covers the tested endpoint, hostname and client path, not every proxy or region. If your app also serves a browser, verify the browser separately. For work that must outlive a disconnected client, use a job and store its progress instead of relying only on the open HTTP response.
Questions
- How do I deploy FastAPI Server-Sent Events on Gregale?
Return text/event-stream from a FastAPI StreamingResponse and enable response streaming with gregale app update <app> --streaming-enabled. Gregale checks plan eligibility when you change the setting.
- How can I tell if my SSE response is buffered?
Have your test endpoint send events a known interval apart. Measure the arrival of each complete event and when the response ends. If the events all arrive near the end, the path has not demonstrated incremental delivery.
- Does curl -N fix SSE buffering at a reverse proxy?
No. It disables curl output buffering, not buffering or compression in an upstream server or proxy.