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 (Server Sent Events) connecting issues #903

Open
GoldenGamerLP opened this issue Oct 19, 2024 · 2 comments
Open

SSE (Server Sent Events) connecting issues #903

GoldenGamerLP opened this issue Oct 19, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@GoldenGamerLP
Copy link

Environment

Using NodeJs 21.6.2 and deployed on Vercel. Using chrome Version 124.0.6367.61 (Offizieller Build)

Reproduction

 * import { createEventStream, sendEventStream } from "h3";
 *
 * eventHandler((event) => {
 *   const eventStream = createEventStream(event);
 *
 *   // Send a message every second
 *   const interval = setInterval(async () => {
 *     await eventStream.push("Hello world");
 *   }, 1000);
 *
 *   // cleanup the interval and close the stream when the connection is terminated
 *   eventStream.onClosed(async () => {
 *     console.log("closing SSE...");
 *     clearInterval(interval);
 *     await eventStream.close();
 *   });
 *
 *   return eventStream.send();
 * });
 * ```
 
 - This is the default example

### Describe the bug

In a nutshell: The time it takes the client to connect with the SSE is between 10 and  up to 30 seconds. I dont know whats going on, but its not usable right now. I hope someone knows whats keeping the time to connect so high.

Regards

Alex

### Additional context

The source code on which Iam working on, but has the same issues:

import { addSyncClient, removeSyncClient } from "~/server/utils/sync";

export default eventHandler(async (event) => {
const { boardId } = getRouterParams(event);
const { uniqueFingerprint } = getQuery(event);

if (!boardId || !uniqueFingerprint || typeof uniqueFingerprint !== "string") {
throw createError({
status: 400,
statusText: "Bad Request",
});
}

const eventStream = createEventStream(event);

// Add the client to the sync clients
addSyncClient(uniqueFingerprint, boardId.toString(), event.context.user ?? undefined, eventStream);

// Implement keep-alive mechanism
const keepAliveInterval = setInterval(() => {
eventStream.push("\n\nkeep-alive\n\n");
}, 10000); // Send a keep-alive comment every 10 seconds

eventStream.onClosed(async () => {
clearInterval(keepAliveInterval); // Clear the keep-alive interval
removeSyncClient(boardId.toString(), uniqueFingerprint);
await eventStream.close();
});

return eventStream.send();
});



### Logs

_No response_
@GoldenGamerLP GoldenGamerLP added the bug Something isn't working label Oct 19, 2024
@GoldenGamerLP
Copy link
Author

New clue: I've figured that the "return #send()" at the end of the event handler is the problem. On vercel, this methods times out.

@GoldenGamerLP
Copy link
Author

Found the problem: https://stackoverflow.com/questions/76204143/sse-works-locally-but-not-when-deployed-to-vercel

My question now is, is there a way to implement the vercel/streaming api into h3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant