Skip to content

Commit

Permalink
docs: add an SSE example
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanong committed Aug 31, 2024
1 parent 0d9f062 commit 7aad3d3
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions docs/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- [Async operations](#async-operations)
- [Debugging Koa](#debugging-koa)
- [HTTP2](#http2)
- [Server-Side Events](#server-side-events)

## Writing Middleware

Expand Down Expand Up @@ -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))
})
```

0 comments on commit 7aad3d3

Please sign in to comment.