Stream
To return a response streaming out of the box by using a generator function with yield
keyword.
typescript
import { Elysia } from 'elysia'
const app = new Elysia()
.get('/ok', function* () {
yield 1
yield 2
yield 3
})
This this example, we may stream a response by using yield
keyword.
Abort
While streaming a response, it's common that request may be cancelled before the response is fully streamed.
Elysia will automatically stop the generator function when the request is cancelled.
Eden
Eden will will interpret a stream response as AsyncGenerator
typescript
import { Elysia } from 'elysia'
import { treaty } from '@elysiajs/eden'
const app = new Elysia()
.get('/ok', function* () {
yield 1
yield 2
yield 3
})
const { data, error } = await treaty(app).ok.get()
if (error) throw error
for await (const chunk of data)
console.log(chunk)