|
| 1 | +--- |
| 2 | +title: Configure API for server-sent events in Azure API Management |
| 3 | +description: How to configure an API for server-sent events (SSE) in Azure API Management |
| 4 | +author: dlepow |
| 5 | +ms.service: api-management |
| 6 | +ms.topic: how-to |
| 7 | +ms.author: danlep |
| 8 | +ms.date: 02/24/2022 |
| 9 | +--- |
| 10 | + |
| 11 | +# Configure API for server-sent events |
| 12 | + |
| 13 | +This article provides guidelines for configuring an API in API Management that implements server-sent events (SSE). SSE is based on the HTML5 `EventSource` standard for streaming (pushing) data automatically to a client over HTTP after a client has established a connection. |
| 14 | + |
| 15 | +> [!TIP] |
| 16 | +> API Management also provides native support for [WebSocket APIs](websocket-api.md), which keep a single, persistent, bidrectional connection open between a client and server. |
| 17 | +
|
| 18 | +## Prerequisites |
| 19 | + |
| 20 | +- An existing API Management instance. [Create one if you haven't already](get-started-create-service-instance.md). |
| 21 | +- An API that implements SSE. [Import and publish](import-and-publish.md) the API to your API Management instance using one of the supported import methods. |
| 22 | + |
| 23 | +[!INCLUDE [premium-dev-standard-basic.md](../../includes/api-management-availability-premium-dev-standard-basic.md)] |
| 24 | + |
| 25 | +## Guidelines for SSE |
| 26 | + |
| 27 | +Follow these guidelines when using API Management to reach a backend API that implements SSE. |
| 28 | + |
| 29 | +* **Choose service tier for long-running HTTP connections** - SSE relies on a long-running HTTP connection. Long-running connections are supported in the dedicated API Management tiers, but not in the Consumption tier. |
| 30 | + |
| 31 | +* **Keep idle connections alive** - If a connection between client and backend could be idle for 4 minutes or longer, implement a mechanism to keep the connection alive. For example, enable a TCP keepalive signal at the backend of the connection, or send traffic from the client side at least once per 4 minutes. |
| 32 | + |
| 33 | + This configuration is needed to override the idle session timeout of 4 minutes that is enforced by the Azure Load Balancer, which is used in the API Management infrastructure. |
| 34 | + |
| 35 | +* **Relay events immediately to clients** - Turn off response buffering on the [`forward-request` policy](api-management-advanced-policies.md#ForwardRequest) so that events are immediately relayed to the clients. For example: |
| 36 | + |
| 37 | + ```xml |
| 38 | + <forward-request timeout="120" fail-on-error-status-code="true" buffer-response="false"/> |
| 39 | + ``` |
| 40 | + |
| 41 | +* **Avoid other policies that buffer responses** - Certain policies such as [`validate-content`](validation-policies.md#validate-content) can also buffer response content and shouldn't be used with APIs that implement SSE. |
| 42 | + |
| 43 | +* **Disable response caching** - To ensure that notifications to the client are timely, verify that [response caching](api-management-howto-cache.md) isn't enabled. For more information, see [API Management caching policies](api-management-caching-policies.md). |
| 44 | + |
| 45 | +* **Test API under load** - Follow general practices to test your API under load to detect performance or configuration issues before going into production. |
| 46 | + |
| 47 | +## Next steps |
| 48 | + |
| 49 | +* Learn more about [configuring policies](/api-management-howto-policies.md) in API Management. |
| 50 | +* Learn about API Management [capacity](api-management-capacity.md). |
0 commit comments