Skip to content

Node V2: proper server streaming client abort handling #1216

Discussion options

You must be logged in to vote

Thank you @srikrsna-buf. Here is my current solution, following the input (#1217), in case some one else needs something similar:
Helper function

export function abortPromise(signal: AbortSignal) {
  return new Promise<never>((_, reject) => {
    signal.addEventListener("abort", () => {
      reject(new Error("The stream has been destroyed"));
    });
  });
}

Stream generator:

export async function* myStream(
  req: AccountRequest,
  context: HandlerContext,
) {
  console.log(`Connected client with id ${req.id}.`);
  const abort = abortPromise(context.signal);
  try {
    while (true) {
      yield Promise.race([abort, getStreamPromise()]);
    }
  } catch (error) {
    console.error((error 

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by dionysiusmarquis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants