From 7ce8dd8c395326e20892a9f31763840fd680fbbc Mon Sep 17 00:00:00 2001 From: Jim Wagner Date: Thu, 2 Apr 2026 13:13:12 +0200 Subject: [PATCH] docs: mention how to change SSE keep-alive ping interval --- docs/en/docs/tutorial/server-sent-events.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/docs/en/docs/tutorial/server-sent-events.md b/docs/en/docs/tutorial/server-sent-events.md index d264f8536f..1f9faa66d0 100644 --- a/docs/en/docs/tutorial/server-sent-events.md +++ b/docs/en/docs/tutorial/server-sent-events.md @@ -113,7 +113,13 @@ This is useful for protocols like [MCP](https://modelcontextprotocol.io) that st FastAPI implements some SSE best practices out of the box. -* Send a **"keep alive" `ping` comment** every 15 seconds when there hasn't been any message, to prevent some proxies from closing the connection, as suggested in the [HTML specification: Server-Sent Events](https://html.spec.whatwg.org/multipage/server-sent-events.html#authoring-notes). +* Send a **"keep alive" `ping` comment** every 15 seconds when there hasn't been any message, to prevent some proxies from closing the connection, as suggested in the [HTML specification: Server-Sent Events](https://html.spec.whatwg.org/multipage/server-sent-events.html#authoring-notes). You can change this interval by setting `fastapi.sse._PING_INTERVAL` (in seconds) at the top of your main application file, before any SSE responses are created: + + ```python + # main.py + import fastapi.sse + fastapi.sse._PING_INTERVAL = 3.0 + ``` * Set the `Cache-Control: no-cache` header to **prevent caching** of the stream. * Set a special header `X-Accel-Buffering: no` to **prevent buffering** in some proxies like Nginx.