Skip to content

Commit bccd0b1

Browse files
svozzasdangol
andauthored
docs(event-handler): add response streaming docs (#4786)
Co-authored-by: Swopnil Dangol <[email protected]>
1 parent 2279f9b commit bccd0b1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

docs/features/event-handler/rest.md

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -595,11 +595,31 @@ For complete control you can return an `APIGatewayProxyEvent` (`v1` or `v2`) and
595595

596596
### Response streaming
597597

598-
!!! note "Coming soon"
598+
!!! note "Compatibility"
599+
Response streaming is only available for [API Gateway REST APIs](https://docs.aws.amazon.com/apigateway/latest/developerguide/response-transfer-mode.html){target="_blank"}
600+
and [Lambda function URLs](https://docs.aws.amazon.com/lambda/latest/dg/configuration-response-streaming.html){target="_blank"}.
601+
602+
You can send responses to the client using HTTP streaming by wrapping your router with the `streamify` function to turn all the associated route handlers into stream compatible handlers. This is useful when you need to send large payloads or want to start sending data before the entire response is ready.
603+
604+
In order to gain the most benefit, you should return either a readable [Nodejs stream](https://nodejs.org/api/stream.html#readable-streams){target="_blank"},
605+
a duplex [Nodejs stream](https://nodejs.org/api/stream.html#class-streamduplex){target="_blank"}, or
606+
a [Web stream](https://developer.mozilla.org/en-US/docs/Web/API/Streams_API){target="_blank"} from your handlers. However, you can also return
607+
other types and these will also be delivered via HTTP streaming.
608+
609+
=== "index.ts"
610+
611+
```ts hl_lines="3 17"
612+
--8<-- "examples/snippets/event-handler/rest/advanced_response_streaming.ts:4"
613+
```
614+
615+
!!! tip "When to use streaming"
616+
Consider response streaming when:
599617

600-
At the moment, Event Handler does not support streaming responses. This means that the entire response must be generated and returned by the route handler before it can be sent to the client.
618+
- Returning large payloads (> 6MB)
619+
- Processing data that can be sent incrementally
620+
- Reducing time-to-first-byte for long-running operations is a requirement
601621

602-
Please [check this issue](https://github.com/aws-powertools/powertools-lambda-typescript/issues/4476) for more details and add 👍 if you would like us to prioritize it.
622+
For most use cases, the standard `resolve` method is sufficient.
603623

604624
### Debug mode
605625

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
declare function createVideoStream(): Readable;
2+
3+
import type { Readable } from 'node:stream';
4+
import {
5+
Router,
6+
streamify,
7+
} from '@aws-lambda-powertools/event-handler/experimental-rest';
8+
9+
const app = new Router();
10+
11+
app.get('/video-stream', async (reqCtx) => {
12+
reqCtx.res.headers.set('content-type', 'video/mp4');
13+
return createVideoStream();
14+
});
15+
16+
app.get('/hello', () => {
17+
return { message: 'Hello World' };
18+
});
19+
20+
export const handler = streamify(app);

0 commit comments

Comments
 (0)