Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSE connection closes when server uses await operations #82

Open
Scover-w opened this issue Oct 30, 2024 · 0 comments
Open

SSE connection closes when server uses await operations #82

Scover-w opened this issue Oct 30, 2024 · 0 comments

Comments

@Scover-w
Copy link

SSE connection closes when server uses await operations

Description

I've encountered a behavior when using sse.js in conjunction with async/await patterns on my express server side. The connection consistently triggering the 'close' event whenever the server executes an await operation. The issue is that the client can't close itself the connection because the 'close' event of the server has already been called. This lead to an unexpected behavior where the server continue its execution even if the client don't want to.

Reproduction

Server code demonstrating the issue:

app.get('/events', async (req, res) => {
  res.setHeader('Connection', 'keep-alive');
  res.setHeader('Cache-Control', 'no-cache');
  res.setHeader('Content-Type', 'text/event-stream');
  res.flushHeaders();

   await wait(1);// Connection closes here
   res.write('data: event\n\n');
});

function wait(ms: number): Promise<any>
{
    return new Promise(resolve => setTimeout(resolve, ms));
}  

Client code:

const sse = new SSE('http://localhost:3000/events');

Event Source

The same server code works perfectly with the native EventSource implementation. The connection remains open when using await operations, suggesting this might be related to sse.js.

Potential Lead

I checked with wireshark to know who was closing the connection and it seems that nobody "close" it, in the sense that nobody send [FIN]. However, the server send a 200 OK [Last Chunk] when the await is called, suggesting that the issue comes from the server. Claude suggested that it's because the XHR 'load' event is triggered prematurely but I don't know what it's worth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant