Is there a way to return multiple responses from the api route
Answered
Dutch Smoushond posted this in #help-forum
Dutch SmoushondOP
Consider the image below. Every variable is making a database query on a heavy database so some queries might take a lot of time while some maybe quick.
Right now we are waiting for all the queries to end and then returning all the repsonse at once this is taking around 3 to 4 minutes. Instead what I am aiming for is to return a part of the data as soon as the query is finished so i can show it on the client while also continue with the other queries. Is making multiple api routes the only way to achieve this or there is something else that I can do
Right now we are waiting for all the queries to end and then returning all the repsonse at once this is taking around 3 to 4 minutes. Instead what I am aiming for is to return a part of the data as soon as the query is finished so i can show it on the client while also continue with the other queries. Is making multiple api routes the only way to achieve this or there is something else that I can do
Answered by James4u
yeah, you need to break down your api into smaller ones - in most cases, it should be the most appropriate approach
18 Replies
@Dutch Smoushond Consider the image below. Every variable is making a database query on a heavy database so some queries might take a lot of time while some maybe quick.
Right now we are waiting for all the queries to end and then returning all the repsonse at once this is taking around 3 to 4 minutes. Instead what I am aiming for is to return a part of the data as soon as the query is finished so i can show it on the client while also continue with the other queries. Is making multiple api routes the only way to achieve this or there is something else that I can do
Kromfohrländer
SSE seems to be the solution imo
In a nutshell:
- Set up streaming response headers
- Create an async generator/stream to send data chunks
- Send each query result as it completes using
- Client uses
In a nutshell:
- Set up streaming response headers
- Create an async generator/stream to send data chunks
- Send each query result as it completes using
controller.enqueue()
- Client uses
EventSource
to receive streamed data"@Dutch Smoushond Consider the image below. Every variable is making a database query on a heavy database so some queries might take a lot of time while some maybe quick.
Right now we are waiting for all the queries to end and then returning all the repsonse at once this is taking around 3 to 4 minutes. Instead what I am aiming for is to return a part of the data as soon as the query is finished so i can show it on the client while also continue with the other queries. Is making multiple api routes the only way to achieve this or there is something else that I can do
yeah, you need to break down your api into smaller ones - in most cases, it should be the most appropriate approach
Answer
@James4u yeah, you need to break down your api into smaller ones - in most cases, it should be the most appropriate approach
Kromfohrländer
that'd work too 👍 (and is much more practical)
Streaming is something for different purpose
@James4u Streaming is something for different purpose
Kromfohrländer
can it not be used here??
did I misunderstand the question 🤔
ah yeah it seems so
at least according to OP's question
Kromfohrländer
ignore me ^^
@James4u yeah, you need to break down your api into smaller ones - in most cases, it should be the most appropriate approach
Dutch SmoushondOP
cool thanks for the info
@Kromfohrländer https://www.w3schools.com/html/html5_serversentevents.asp
Dutch SmoushondOP
TIL, will look more into this later
@Dutch Smoushond cool thanks for the info
I believe one api route shouldn't be responsible for the multiple queries
Dutch SmoushondOP
but @James4u is sse something you should use when you need real time response / live response
@James4u I believe one api route shouldn't be responsible for the multiple queries
Dutch SmoushondOP
hmm I was thinking the same actually I just got the task of optimizing things as they are slow
@Dutch Smoushond hmm I was thinking the same actually I just got the task of optimizing things as they are slow
yeah breaking them down should be good enough to make it faster
also utlize server components as well - fetch data and pre-render on the server
@James4u also utlize server components as well - fetch data and pre-render on the server
Dutch SmoushondOP
yeah that's the ideal way of fetching from an api right,
always fetch server side when you can if not then only fetch on client
always fetch server side when you can if not then only fetch on client