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

handle closed connection #6

Open
david-plugge opened this issue Nov 26, 2023 · 3 comments
Open

handle closed connection #6

david-plugge opened this issue Nov 26, 2023 · 3 comments

Comments

@david-plugge
Copy link

When the client closes the connection, the server maybe has to do something like clearing an interval.

app.get('/live', () => {
	return new Stream(async (stream) => {
		setInterval(() => {
			stream.send('hello')
		}, 1000)
	})
})

How am i supposed to achive that with the current version? I´d expect to be able to return a callback that will be called when the connection is closed to handle such stuff.

@ajit283
Copy link

ajit283 commented Nov 30, 2023

Ok, I found out how to do it now: you need to take the raw Request object from the context of the function (https://elysiajs.com/concept/handler.html#context) and add an eventListener to it:

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

@VaronLaStrauss
Copy link

To be more precise, we could use a custom function like so:

function signalCb(this: AbortSignal, ev: Event) {
   sub.unsubscribe(); // perform unsubscribe from subscribed things
   stream.close(); 
   this.removeEventListener("abort", signalCb); // yes, I am scared
}
request.signal.addEventListener("abort", signalCb, { once: true });

@bogeychan
Copy link

Blocked by #10

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

4 participants