Skip to content

Commit

Permalink
feat: add sse chat endpoint with arbitrary chunks (#12)
Browse files Browse the repository at this point in the history
This change adds a new endpoint which streams server-sent events in
arbitrarily sized chunks to exercise the buffering and scanning
functionality in SDKs.
  • Loading branch information
disintegrator authored Jul 15, 2024
1 parent ad5c46f commit 0c2e53a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func main() {
r.HandleFunc("/eventstreams/multiline", eventstreams.HandleEventStreamMultiLine).Methods(http.MethodPost)
r.HandleFunc("/eventstreams/rich", eventstreams.HandleEventStreamRich).Methods(http.MethodPost)
r.HandleFunc("/eventstreams/chat", eventstreams.HandleEventStreamChat).Methods(http.MethodPost)
r.HandleFunc("/eventstreams/chat-chunked", eventstreams.HandleEventStreamChat).Methods(http.MethodPost)
r.HandleFunc("/eventstreams/differentdataschemas", eventstreams.HandleEventStreamDifferentDataSchemas).Methods(http.MethodPost)
r.HandleFunc("/clientcredentials/token", clientcredentials.HandleTokenRequest).Methods(http.MethodPost)
r.HandleFunc("/clientcredentials/authenticatedrequest", clientcredentials.HandleAuthenticatedRequest).Methods(http.MethodPost)
Expand Down
24 changes: 24 additions & 0 deletions internal/eventstreams/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ import (
"time"
)

func pushChunks(rw http.ResponseWriter, chunks []string) {
for _, chunk := range chunks {
fmt.Fprintln(rw, chunk)

if f, ok := rw.(http.Flusher); ok {
f.Flush()
}

time.Sleep(100 * time.Millisecond)
}
}

func pushEvents(rw http.ResponseWriter, events [][]string) {
for _, event := range events {
for _, line := range event {
Expand Down Expand Up @@ -127,6 +139,18 @@ func HandleEventStreamChat(rw http.ResponseWriter, _ *http.Request) {
})
}

func HandleEventStreamChatChunked(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Add("Content-Type", "text/event-stream")

pushChunks(rw, []string{
"data: {\"content\": ",
"\"Hello\"}\n\ndata: {\"content\": \" \"}",
"data: {\"content\": \"world\"}",
"data: {\"content\": \"!\"}\n\ndata: [DONE]\n",
"\ndata: {\"content\": \"Post sentinel data\"}\n\n",
})
}

func HandleEventStreamDifferentDataSchemas(rw http.ResponseWriter, _ *http.Request) {
rw.Header().Add("Content-Type", "text/event-stream")

Expand Down

0 comments on commit 0c2e53a

Please sign in to comment.