Browse Source

docs: mention how to change SSE keep-alive ping interval

pull/15292/head
Jim Wagner 3 months ago
parent
commit
7ce8dd8c39
  1. 8
      docs/en/docs/tutorial/server-sent-events.md

8
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. 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 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. * Set a special header `X-Accel-Buffering: no` to **prevent buffering** in some proxies like Nginx.

Loading…
Cancel
Save