shawnbu
shawnbu16mo ago

Close a Deno.serve() connection?

Is it possible to close a Deno.serve() connection?
7 Replies
ioB
ioB16mo ago
Do you mean the server or the connection? If you mean the server, you should use the web-standard AbortSignal API:
const controller = new AbortController();
const signal = controller.signal;

Deno.serve({ signal }, () => {
// whenever you want to close the server
controller.abort();
})
const controller = new AbortController();
const signal = controller.signal;

Deno.serve({ signal }, () => {
// whenever you want to close the server
controller.abort();
})
shawnbu
shawnbuOP16mo ago
Oh wow! I thought I found it through unref(), this seems nicer. How would I close the connection outside the request loop?
ioB
ioB16mo ago
You could close the connection outside of the request loop like you'd expect! Just call controller.abort();
shawnbu
shawnbuOP16mo ago
If I have many Deno.servers going on for different ports (which is what I'm doing), how do I map a controller to a specific server? Oh, I pass the signal instance into the server? I see. And then how to pass configs with the signal at the same time? Forgive me, I'm new to Deno and trying to figure it all how.
ioB
ioB16mo ago
Presumably you'd want to shut down the servers at different times? If that's the case, I think you got it. If that's not the case, you only need one abort controller!
shawnbu
shawnbuOP16mo ago
Thanks so much for helping me with your answers.
Yes, at different times. I can remote send a command to do it. only thing is how to initialize server with the signal and config props at the same time (for port)
lcasdev
lcasdev16mo ago
Next to the signal, you can specify a port property