Server-Sent Events (SSE) Nextjs in Amazon


Hello Team, I’m reaching out to seek your expertise regarding a technical challenge we’ve encountered related to streaming functionality in our web application. Our setup involves a Next.js backend serving real-time data to a React frontend through Server-Sent Events (SSE), a mechanism allowing the server to push updates to the client browser.|

Issue Overview: We’ve successfully implemented SSE in our development environment, where the streaming from our Next.js server to the React client works seamlessly. However, upon deploying our application to our staging environment (Cloudformation and ECS), we noticed that the streaming functionality is not operating as expected. Specifically, the React frontend is not receiving the streamed data from the Next.js backend, despite the connection seemingly being established without errors.

Troubleshooting Steps Taken: Local Testing: To isolate the issue, we performed local tests by running both the Next.js backend and React frontend in a controlled environment(by using the same image from the ECR and the same .env we have in staging). In this setup, the streaming functionality worked flawlessly, indicating that the problem likely lies within our staging environment’s configuration rather than the application code itself.

Infrastructure Considerations: Given the discrepancy between local and staging environment behaviors, we suspect the issue may be related to our staging environment’s infrastructure configuration. This could involve specific settings in our load balancers, network policies, or other middleware that might interfere with SSE’s ability to maintain a persistent connection. resume, not idea if could be missing soemthing in the AWS Load Balancer or in another place.

Thank you very much for your time and assistance. We're hopeful that with your collective expertise, we can identify and remedy the root cause of this streaming issue.

asked 22 days ago90 views
2 Answers

Hello . Please kindly note that due to the complexity of the case (as it involves multiple services) I would suggest that you open a support case with AWS, so that our skilled engineers can provide informed guidance on this issue. Please note that you can open the case with the ECS team initially.

answered 19 days ago

Does your application set the 'X-Accel-Buffering': 'no' HTTP header? Because e.g. running a SSE producing server behind NGINX requires that header as otherwise NGINX would close the connection or something similar. You could try running your application locally with NGINX in front of it as a reverse proxy to verify if that’s the case.


upstream api {
    server api:8080;

server {
    listen 80;

    location /api {
        proxy_pass http://api;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host $http_host;
        proxy_redirect off;

and then access your SSE backend through NGINX.

I guess it’s possible that the ALB behaves similarly.

answered 6 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions