|
| 1 | +--- |
| 2 | +title: Stream Plugin - ElysiaJS |
| 3 | +head: |
| 4 | + - - meta |
| 5 | + - property: 'og:title' |
| 6 | + content: Stream Plugin - ElysiaJS |
| 7 | + |
| 8 | + - - meta |
| 9 | + - name: 'description' |
| 10 | + content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/static". |
| 11 | + |
| 12 | + - - meta |
| 13 | + - name: 'og:description' |
| 14 | + content: Plugin for Elysia that add support for streaming response and Server Sent Event, eg. OpenAI integration. Start by installing the plugin with "bun add @elysiajs/static". |
| 15 | +--- |
| 16 | + |
| 17 | +# Stream Plugin |
| 18 | +This plugin add support for streaming response or sending Server Sent Event back to client. |
| 19 | + |
| 20 | +Install with: |
| 21 | +```bash |
| 22 | +bun add @elysiajs/stream |
| 23 | +``` |
| 24 | + |
| 25 | +Then use it: |
| 26 | +```typescript |
| 27 | +import { Elysia } from 'elysia' |
| 28 | +import { Stream } from '@elysiajs/stream' |
| 29 | + |
| 30 | +new Elysia() |
| 31 | + .get('/', () => new Stream(async (stream) => { |
| 32 | + stream.send('hello') |
| 33 | + |
| 34 | + await stream.wait(1000) |
| 35 | + stream.send('world') |
| 36 | + |
| 37 | + stream.close() |
| 38 | + })) |
| 39 | + .listen(8080) |
| 40 | +``` |
| 41 | + |
| 42 | +By default, `Stream` will return `Response` with `content-type` of `text/event-stream; charset=utf8`. |
| 43 | + |
| 44 | +You can pass `Iterable` or `AsyncIterable` to `Stream` to stream content back to client. |
| 45 | + |
| 46 | +## Constructor |
| 47 | +Below is the constructor parameter accept by `Stream`: |
| 48 | +- Automatic: Iterable / AsyncIterable |
| 49 | +- Manual: Callback of `(stream: this) => unknown` or `undefined` |
| 50 | + |
| 51 | +## Method |
| 52 | +Below is the method provided by `Stream`: |
| 53 | + |
| 54 | +### send |
| 55 | +Enqueue data to stream to send back to client |
| 56 | + |
| 57 | +### close |
| 58 | +Close the stream |
| 59 | + |
| 60 | +### wait |
| 61 | +Return a promise that resolved in provided value in ms |
| 62 | + |
| 63 | +### value |
| 64 | +Inner value of the `ReadableStream` |
| 65 | + |
| 66 | +# Example |
| 67 | + |
| 68 | +## OpenAI |
| 69 | +Automatic mode is triggered when parameter is either `Iterable` or `AsyncIterable` streaming response back to client automatically. |
| 70 | + |
| 71 | +Below is the example to integrate ChatGPT to Elysia. |
| 72 | + |
| 73 | +```ts |
| 74 | +new Elysia() |
| 75 | + .post( |
| 76 | + '/ai', |
| 77 | + ({ body, query: { prompt } }) => |
| 78 | + new Stream( |
| 79 | + openai.chat.completions.create({ |
| 80 | + model: 'gpt-3.5-turbo', |
| 81 | + stream: true, |
| 82 | + messages: [{ |
| 83 | + role: 'user', |
| 84 | + content: prompt |
| 85 | + }] |
| 86 | + }) |
| 87 | + ) |
| 88 | + ) |
| 89 | +``` |
| 90 | + |
| 91 | +By default [openai](https://npmjs.com/package/openai) chatGPT completion return `AsyncIterable` so you should be able to wrap the OpenAI in `Stream`. |
| 92 | + |
| 93 | +## Server Sent Event |
| 94 | +Manual mode is triggered when parameter is either `callback` or `undefined`, allowing you to control the stream. |
| 95 | + |
| 96 | +### callback-based |
| 97 | +Below is an example to create Server Sent Event endpoint using constructor callback |
| 98 | + |
| 99 | +```ts |
| 100 | +new Elysia() |
| 101 | + .get('/source', () => |
| 102 | + new Stream((stream) => { |
| 103 | + const interval = setInterval(() => { |
| 104 | + stream.send('hello world') |
| 105 | + }, 500) |
| 106 | + |
| 107 | + setTimeout(() => { |
| 108 | + clearInterval(interval) |
| 109 | + stream.close() |
| 110 | + }, 3000) |
| 111 | + }) |
| 112 | + ) |
| 113 | +``` |
| 114 | + |
| 115 | +### value-based |
| 116 | +Below is an example to create Server Sent Event endpoint using value-based |
| 117 | + |
| 118 | +```ts |
| 119 | +new Elysia() |
| 120 | + .get('/source', () => { |
| 121 | + const stream = new Stream() |
| 122 | + |
| 123 | + const interval = setInterval(() => { |
| 124 | + stream.send('hello world') |
| 125 | + }, 500) |
| 126 | + |
| 127 | + setTimeout(() => { |
| 128 | + clearInterval(interval) |
| 129 | + stream.close() |
| 130 | + }, 3000) |
| 131 | + |
| 132 | + return stream |
| 133 | + }) |
| 134 | +``` |
0 commit comments