diff --git a/docs/guide.md b/docs/guide.md index 4ca0cd4d8..fd7d35123 100644 --- a/docs/guide.md +++ b/docs/guide.md @@ -16,6 +16,7 @@ - [Async operations](#async-operations) - [Debugging Koa](#debugging-koa) - [HTTP2](#http2) + - [Server-Side Events](#server-side-events) ## Writing Middleware @@ -267,3 +268,48 @@ const serverOptions = { const server = http2.createSecureServer(serverOptions, onRequestHandler); ``` + +## Server-Side Events + +An example of using server-side events with Koa: + +```js +import PassThrough from 'stream' +import OpenAI from 'openai' +import Koa from 'koa' + +const openai = new OpenAI({ + apiKey: process.env.OPENAI_API_KEY, +}) + +const app = new Koa() + +app.use(async (ctx, next) => { // TODO: use your favorite routing library + const { message } = await ctx.request.json() // this example uses koa-body-parsers + + const stream = await client.chat.completions.create({ + model: 'gpt-4o-mini', + messages: [{ role: 'user', content: messages }], + stream: true, + }) + + ctx.response.type = 'text/event-stream' + const body = ctx.body = new PassThrough() + + // streaming needs to be handled in a separate event loop + ;(async () => { + for await (const chunk of stream) { + const content = chunk.choices[0]?.delta?.content + if (!content) continue + + body.write(`data: ${JSON.stringify({ + delta: { + content: chunk.choices[0].delta.content || '' + } + })}}\n\n`) + + body.end() + } + })().catch((err) => app.emit('error', err)) +}) +```