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

Fixed by #23425 — how to use req.signal to check that the response was delivered to the client? #23678

Closed
leonbakadorov opened this issue May 3, 2024 · 6 comments

Comments

@leonbakadorov
Copy link

How can you make sure that the response is guaranteed to be delivered to the client?

Previously I suggested something like this:

// my idea
if (res.done) {
    // code
}

Now I guess it works somehow through req.onabort?

// your implementation
if (!req.onabort) {
    // code
}

Explain how this should work, because I didn't find it in the API.

@leonbakadorov
Copy link
Author

I get the feeling that req.onabort does not contribute at all to this task.

@mmastrac
Copy link
Member

mmastrac commented May 3, 2024

You can use the signal like so:

Deno.serve((req) => {
  req.signal.onabort = () => console.log("done");
  return new Response("ok");
});

It'll be triggered when the response has been fully delivered to the http layer.

@mmastrac
Copy link
Member

mmastrac commented May 3, 2024

It also works with a stream like so:

Deno.serve((req) => {
  req.signal.onabort = () => console.log("done");
  return new Response(
    new ReadableStream({
      start(c) {
        c.enqueue(new TextEncoder().encode('ok\n'));
        c.close();
      },
    }),
  );
});

@leonbakadorov
Copy link
Author

mmastrac,
And if the client’s Internet or device simply disconnects at the time of the request, in this case the connection will be disconnected and “done” will be displayed in the console?

@leonbakadorov
Copy link
Author

leonbakadorov commented May 4, 2024

Oh, you have a great example on your blog on Deno 1.43:

Deno.serve((req, info) => {
  info.completed.then(() => {
    console.log("Response sent successfuly!");
  }).catch(() => {
    console.error("Failed sending the response.");
  });
  return new Response("Hello world");
});

I think this is what I was looking for, right?

@mmastrac
Copy link
Member

mmastrac commented May 4, 2024

Correct on both counts. The completed signal will also work.

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

3 participants