Skip to content

Setting up with sveltekit #100

Description

@devcsrj

Hi @MatthewWid ! I came across your library and was trying it out with Sveltekit. The setup is straightforward:

export const GET: RequestHandler = async ({ request }) => {
  return createResponse(request, (session) => {
     session.push('Hello world');
  });
};

This works, however, the session never seems to be disconnected properly. For instance, setting up a ticker like this:

export const GET: RequestHandler = async ({ request }) => {
  return createResponse(request, (session) => {
    const tick = setInterval(() => {
      session.push(`The time is: ${new Date()}`);
    }, 1000);

    session.on("disconnected", () => {
      clearInterval(tick);
    });
  });
};

When the client disconnects (e.g.: navigating , or refreshing page), the Session is still active and will try to still send the message. The server will crash with:

UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "undefined".
    at throwUnhandledRejectionsMode (node:internal/process/promises:392:7)
    at processPromiseRejections (node:internal/process/promises:475:17)
    at process.processTicksAndRejections (node:internal/process/task_queues:106:32) {
  code: 'ERR_UNHANDLED_REJECTION'
}

Minimum repo: better-sse-sveltekit

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions