Event on client disconnect/abort in a streaming response

Open
#145 1 comment 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
42/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
aws, node.js, typescript

Research direction

Start at the awslambda.streamifyResponse entry point and inspect the responseStream lifecycle, especially the attempted close listener shown in the issue. Determine how a client abort or disconnect is represented, then verify that an event is emitted during the streaming loop and that its behavior is covered by an appropriate test.

Written by the indexing model from the issue text.

Description

Im wondering if its possible to get any event emitted when a client aborts or disconnects during a streaming response with awslambda.streamifyResponse. I tried with this code, but from my testing it looks like theres no event emitted on the Lambda when that happens:

exports.handler = awslambda.streamifyResponse(
    async (event, responseStream, context) => {
        responseStream.setContentType('text/event-stream')
        responseStream.on("close", ()=> {
          // this never gets emitted at any point
          console.log("responseStream closed");
        });
        for (let i = 0; i < 20; i++) {
            await sleep(1000);
            responseStream.write(`event: ping\n`);
            responseStream.write(`data: ${i}\n\n`);
        }
        responseStream.end();
    }
);

This would be an awesome feature to add in the age of SSR streaming, LLM's and more.

Dominant language
TypeScript
Stars
224
Forks
60
Avg merge
26m
Merged PRs (30d)
2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aws/aws-lambda-nodejs-runtime-interface-client

All issues in aws/aws-lambda-nodejs-runtime-interface-client

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.