trpc / trpc

πŸ§™β€β™€οΈ Move Fast and Break Nothing. End-to-end typesafe APIs made easy.
https://tRPC.io
MIT License
35.13k stars 1.26k forks source link

feat: Add support for aws-lambda on httpBatchStreamLink #4474

Open H4ad opened 1 year ago

H4ad commented 1 year ago

Describe the feature you'd like to request

httpBatchStreamLink was implemented on this https://github.com/trpc/trpc/pull/4347

Currently, the built-in AWS Lambda Adapter for tRPC didn't provide support for AWS Lambda Response Streaming, so I'm opening this issue just to keep track of the future support of this functionality.

Describe the solution you'd like to see

The ideal solution should be something like this:

import { CreateAWSLambdaContextOptions, awsLambdaRequestHandler } from '@trpc/server/adapters/aws-lambda-stream';
const appRouter = /* ... */;
// created for each request
const createContext = ({
  event,
  context,
}: CreateAWSLambdaContextOptions<APIGatewayProxyEventV2>) => ({}) // no context
type Context = trpc.inferAsyncReturnType<typeof createContext>;
export const handler = awsLambdaRequestStreamHandler({
  router: appRouter,
  createContext,
})

Describe alternate solutions

I'm the maintainer of Serverless Adapter, a library to connect any serverless environment with any NodeJS Framework.

I already added support for AWS Lambda Response Stream (docs), and I also support tRPC (I still need to update my library to add support to the latest version) but I think it will work without any issue this new feature using my library.

If you want to add support for response streaming on the internal built-in, I suggest for who are interested in implementing, read the following files:

Of course, the final implementation can be different but this is just my suggestion.

Additional information

No response

πŸ‘¨β€πŸ‘§β€πŸ‘¦ Contributing

Funding

Fund with Polar

dbrxnds commented 1 year ago

Hi @H4ad, is there a way to get your solution working with Next.js/Vercel?

H4ad commented 1 year ago

@dbrxnds In theory, you just need to create an adapter for Vercel, in order to parse in/out the Request/Response.

Create an issue on my Serverless Adapter repository with a minimal example of the stream usage on Vercel, so I can try add support.

dbrxnds commented 1 year ago

Hi @H4ad, streaming actually works with app router on Vercel when using route handlers, as described here

nicolassanmar commented 1 year ago

@dbrxnds Do you know how to make streaming work using the page router on Vercel? Is that possible?

helmturner commented 1 year ago

@H4ad We've got an open discussion on discord about serverless runtimes and new, http-based streaming features. Care to weigh in?

helmturner commented 1 year ago

@nicolassanmar I believe if you use edge routes it 'just works'

edit: If, that is, you're referring to the tRPC streaming features, not those in NextJS (which are new to app router)

tmokmss commented 3 months ago

Using Lambda Web Adapter with AWS_LWA_INVOKE_MODE=response_stream should just work. The Lambda extension converts transfer-encoding: chunked response to Lambda streaming protocol.