ASP Windows with Java 17 SE Backend Support for Server Sent Events
Is it possible for ASP Windows with a Java SE backend to support Server Sent Events? Locally, everything functions properly when running the standalone Spring Boot JAR, the events are sent out one at a time as they occur. However, when deploying the same JAR and configurations to an Azure ASP Windows environment, the connection remains open as expected, but the event message payloads are buffered in large chunks instead of being sent one at a time. When enough events are generated to receive this large chunk, the last event is typically cut off and not fully delivered. I was able to confirm that when executing curl directly on the azure container, the content streams as expected. Initially, I suspected that the issue was related to the application gateway, but even when hitting the container directly i get the same behavior. The connection is open, but the stream is buffered. Any insights or recommendations would be greatly appreciated.
Azure App Service
-
Bhargavi Naragani • 735 Reputation points • Microsoft Vendor
2025-02-18T07:57:44.0466667+00:00 Hi @Ben Frisoni,
Server-Sent Events (SSE) can be supported in an ASP Windows environment with a Java SE backend, but there are specific configurations and guidelines to ensure that the events are not buffered. In your case, it seems that the buffering issue might be related to the response buffering settings in the API Management or the Application Gateway.
To address the buffering issue, consider the following steps:
- Ensure that the response buffering is disabled in your API Management configuration. For example, using the
forward-request
policy, you can setbuffer-response="false"
to relay events immediately to clients. - If your connection could be idle for extended periods, implement mechanisms to keep the connection alive, such as enabling TCP keepalive signals.
- Certain policies, like
validate-content
, can inadvertently buffer responses. Make sure these are not applied to your API that implements SSE. - If you have logging enabled for Azure Monitor or Application Insights, ensure that request/response body logging is disabled, as this can also lead to unexpected buffering.
- Since you mentioned that the issue persists even when hitting the container directly, it is crucial to verify these configurations in your Azure environment to ensure that they align with the requirements for SSE.
References:
Server-sent events
Configure API for server-sent eventsIf you have any further concerns or queries, please feel free to reach out to us.
If the answer is helpful, kindly Upvote it. - Ensure that the response buffering is disabled in your API Management configuration. For example, using the
-
Ben Frisoni • 0 Reputation points
2025-02-18T16:22:55.7666667+00:00 Thank you for your response @Naragani Bhargavi (Quadrant Technologies) . It seems your response applies to when API management is being used. In my case I am not using API management nor are the azure logs are enabled. Do you have similar suggestions for the App Service windows IIS. I believe that is the particular component that is doing the buffering. As it stands infront of the java backend.
-
Bhargavi Naragani • 735 Reputation points • Microsoft Vendor
2025-02-19T01:38:42.7533333+00:00 Hi @Ben Frisoni,
Since you are not using API Management or Application Gateway, the most probable reason for this behavior is IIS (Internet Information Services), which is in front of your Java backend in an Azure App Service (Windows) deployment. IIS buffers responses by default, which can get in the way of real-time event streaming like SSE.To resolve this, you need to disable response buffering in IIS and ensure that streaming is properly supported. Here’s what you can do:
- IIS buffer responses automatically unless told otherwise. To correct for this:
Add a web.config file to the wwwroot directory of your App Service with the following content. This setup makes sure that IIS doesn't buffer responses and sends events to the client right away.<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <httpProtocol> <customHeaders> <add name="Cache-Control" value="no-cache, no-store, must-revalidate"/> <add name="Pragma" value="no-cache"/> <add name="Expires" value="0"/> </customHeaders> </httpProtocol> <asp> <bufferingLimit>0</bufferingLimit> </asp> <httpRuntime enableKernelOutputCache="false" /> </system.webServer> </configuration>
- SSE (Server-Sent Events) needs to send events in a continuous stream. However, IIS can disrupt this process unless you configure it to use chunked transfer encoding. In your Java Spring Boot application, ensure that your SSE response is set up to include:
response.setHeader("Transfer-Encoding", "chunked");
- If IIS still buffers the responses even after making those adjustments, you might want to consider using WebSockets or setting up a reverse proxy like Nginx to manage SSE traffic directly, bypassing IIS altogether.
- Azure's logging features can sometimes lead to buffering issues. If request body logging is turned on in Application Insights, make sure to disable it.
Reference:
Azure App Service - web.config reference
Using Server-Sent Events with IIS
Hope the above provided information help in better understanding and help you resolve the issue, if you have any further concerns or queries, please feel free to reach out to us.
If the answer is helpful, kindly Upvote it. - IIS buffer responses automatically unless told otherwise. To correct for this:
-
Ben Frisoni • 0 Reputation points
2025-02-19T20:23:32.6966667+00:00 hi @Bhargavi Naragani . Unfortunately the suggested solution did not work.
- web.config changes
- customHeaders -> The spring boot java backend already set these headers. This configuration just resulted in duplicate headers being returned. Nevertheless, this had no impact
- asp.bufferingLimit -> No impact here. Based on the Microsoft documentation this is only for .net backends, NOT java.
- httpRuntime.enableKernelOutputCache -> This did not work. When enabled, the entire site was inaccessible.
- The spring boot java backend, already sets this header. It is visible to the SSE consume when a large buffer is finally delivered.
- Can you please provide the documentation of how IIS can be replaced with Nginx in Azure App Service Plan?
- As I already mentioned previously, Azure logs are not enabled in this environment.
Thanks
- web.config changes
-
Ben Frisoni • 0 Reputation points
2025-02-19T20:29:01.4066667+00:00 Deleted
-
Ben Frisoni • 0 Reputation points
2025-02-19T20:31:08.5433333+00:00 Deleted
-
Bhargavi Naragani • 735 Reputation points • Microsoft Vendor
2025-02-20T04:02:41.5466667+00:00 Hi @Ben Frisoni,
Thank you for confirming and trying out the suggestions.
To use Nginx instead of IIS with Azure App Service, you'll need to switch from the Windows version to the Linux version. This allows you to run Nginx as a reverse proxy within a custom Docker container. Follow the below steps:
- For deploying your Spring Boot application on Azure, use a Linux-based App Service Plan and Docker for Server-Sent Events (SSE).
- Rather than the standard Windows-based App Service Plan, create a Linux one in the Azure portal. This is required for Docker container execution.
- Adjust your Docker container to have Nginx configuration that correctly manages SSE.
- Below there's an example nginx.conf file:
server { listen 80; location /sse { proxy_pass http://localhost:8080; proxy_buffering off; proxy_set_header X-Accel-Buffering no; proxy_set_header Connection keep-alive; } }
- Push your application's container to Azure Container Registry (ACR) or Docker Hub.
- Configure your Azure App Service to pull and run the container.
Host a Custom Nginx in Azure App Service (Linux)
Deploy a Spring Boot application to Linux on Azure App Service
If you can't switch to Linux, another option is to use Azure Spring Apps. It's a service that runs your Spring Boot applications for you, and it doesn't depend on IIS. This means your server-sent events (SSE) should stream smoothly without any buffering problems.
- Deploy your Spring Boot app to Azure Spring Apps.
- Azure Spring Apps will execute your application natively without using IIS.
- This eliminates the necessity for any web server (IIS, Nginx, etc.) because Azure manages networking effectively.
Overview:
https://learn.microsoft.com/en-us/azure/spring-apps/
If you have any further concerns or queries, please feel free to reach out to us.
-
Ben Frisoni • 0 Reputation points
2025-02-21T04:01:57.0966667+00:00 Thanks @Bhargavi Naragani .
Can you confirm the ASP Windows does not support SSE for Java backends, and the only way would be to switch the current Azure product?
-
Bhargavi Naragani • 735 Reputation points • Microsoft Vendor
2025-02-21T05:19:40.8966667+00:00 Hi @Ben Frisoni,
Yes, I confirm that Azure App Service (Windows) does not have complete support for SSE for Java backends because of how IIS buffers requests. As IIS is closely integrated with Windows-based App Services and cannot be disabled or swapped out, SSE responses will always be buffered instead of streamed in real time.
In order to achieve proper SSE support, your best option would be to change over to a different Azure offering which does not sit in front of your Java backend an IIS. You basically have two options, which I’ve already mentioned in the previous responses:
- Azure App Service (Linux) with Nginx, this would let you deploy a Spring Boot app with a reverse proxy (such as Nginx) which properly processes SSE.
Run Java Apps on Azure App Service (Linux) - Azure Spring Apps, this is a managed Spring Boot service that host your Java app natively, without IIS, so that SSE behaves exactly as it would in native Linux.
Deploy Java Apps to Azure Spring Apps
Hope the above provided information help in better understanding and help you resolve the issue, if you have any further concerns or queries, please feel free to reach out to us.
If the answer is helpful, kindly Upvote it. - Azure App Service (Linux) with Nginx, this would let you deploy a Spring Boot app with a reverse proxy (such as Nginx) which properly processes SSE.
Sign in to comment