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

Astro request.signal abort event not called for SSE #12867

Open
1 task done
mihaiandrei97 opened this issue Dec 31, 2024 · 1 comment
Open
1 task done

Astro request.signal abort event not called for SSE #12867

mihaiandrei97 opened this issue Dec 31, 2024 · 1 comment
Labels
needs response Issue needs response from OP needs triage Issue needs to be triaged

Comments

@mihaiandrei97
Copy link

mihaiandrei97 commented Dec 31, 2024

Astro Info

Astro                    v5.1.1
Node                     v20.9.0
System                   macOS (arm64)
Package Manager          npm
Output                   server
Adapter                  @astrojs/node
Integrations             none

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

While trying to implement SSE with node adapter in Astro, it seems the abort event is not triggered on request.signal when the connection is closed.

export const clients = new Set() as Set<(message: string) => void>;

export const GET: APIRoute = ({ request }) => {
  const responseHeaders = {
    "Content-Type": "text/event-stream",
    "Cache-Control": "no-cache",
    Connection: "keep-alive",
  };

  const stream = new ReadableStream({
    start(controller) {
      const encoder = new TextEncoder();

      // Function to send messages to this client
      const send = (message: string) => {
        controller.enqueue(encoder.encode(`data: ${message}\n\n`));
      };

      // Add the send function to the set of clients
      clients.add(send);

      // Send an initial message
      send("Connected to SSE");

      request.signal.addEventListener("abort", () => {
        console.log("Client disconnected ");
        clients.delete(send);
        controller.close();
      });

    },
  });

  return new Response(stream, { headers: responseHeaders });
};

What's the expected result?

The following callback should be called when the client disconnects.

request.signal.addEventListener("abort", () => {
  console.log("Client disconnected ");
});

Link to Minimal Reproducible Example

https://stackblitz.com/edit/github-yi4swteg?file=src%2Fpages%2Fsse.ts

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 31, 2024
@ematipico ematipico added the needs response Issue needs response from OP label Jan 1, 2025
@ematipico
Copy link
Member

Can tell us how to reproduce the issue and how this signal/SSE is supposed to work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs response Issue needs response from OP needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants