From aa960372ca5f4f4fb02a99cd71cb7058cecee12b Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 27 Nov 2024 10:13:28 +0000 Subject: [PATCH] Add docs from https://github.com/gofiber/fiber/commit/31a503f --- docs/core/api/ctx.md | 60 ++++++++++++++++++++++++++++++++++++++++++ docs/core/whats_new.md | 38 ++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) diff --git a/docs/core/api/ctx.md b/docs/core/api/ctx.md index 9a5bf3ccd45..2171a0ba808 100644 --- a/docs/core/api/ctx.md +++ b/docs/core/api/ctx.md @@ -1852,6 +1852,66 @@ app.Get("/", func(c fiber.Ctx) error { }) ``` +## SendStreamWriter + +Sets the response body stream writer. + +:::note +The argument `streamWriter` represents a function that populates +the response body using a buffered stream writer. +::: + +```go title="Signature" +func (c Ctx) SendStreamWriter(streamWriter func(*bufio.Writer)) error +``` + +```go title="Example" +app.Get("/", func (c fiber.Ctx) error { + return c.SendStreamWriter(func(w *bufio.Writer) { + fmt.Fprintf(w, "Hello, World!\n") + }) + // => "Hello, World!" +}) +``` + +:::info +To send data before `streamWriter` returns, you can call `w.Flush()` +on the provided writer. Otherwise, the buffered stream flushes after +`streamWriter` returns. +::: + +:::note +`w.Flush()` will return an error if the client disconnects before `streamWriter` finishes writing a response. +::: + +```go title="Example" +app.Get("/wait", func(c fiber.Ctx) error { + return c.SendStreamWriter(func(w *bufio.Writer) { + // Begin Work + fmt.Fprintf(w, "Please wait for 10 seconds\n") + if err := w.Flush(); err != nil { + log.Print("Client disconnected!") + return + } + + // Send progress over time + time.Sleep(time.Second) + for i := 0; i < 9; i++ { + fmt.Fprintf(w, "Still waiting...\n") + if err := w.Flush(); err != nil { + // If client disconnected, cancel work and finish + log.Print("Client disconnected!") + return + } + time.Sleep(time.Second) + } + + // Finish + fmt.Fprintf(w, "Done!\n") + }) +}) +``` + ## Set Sets the response’s HTTP header field to the specified `key`, `value`. diff --git a/docs/core/whats_new.md b/docs/core/whats_new.md index 8677a0080f8..a222a65ce53 100644 --- a/docs/core/whats_new.md +++ b/docs/core/whats_new.md @@ -268,6 +268,7 @@ DRAFT section - Reset - Schema -> ExpressJs like - SendStream -> ExpressJs like +- SendStreamWriter - SendString -> ExpressJs like - String -> ExpressJs like - ViewBind -> instead of Bind @@ -296,6 +297,43 @@ DRAFT section - UserContext has been renamed to Context which returns a context.Context object. - SetUserContext has been renamed to SetContext. +### SendStreamWriter + +In v3, we added support for buffered streaming by providing the new method `SendStreamWriter()`. + +```go +func (c Ctx) SendStreamWriter(streamWriter func(w *bufio.Writer)) +``` + +With this new method, you can implement: + +- Server-Side Events (SSE) +- Large file downloads +- Live data streaming + +```go +app.Get("/sse", func(c fiber.Ctx) { + c.Set("Content-Type", "text/event-stream") + c.Set("Cache-Control", "no-cache") + c.Set("Connection", "keep-alive") + c.Set("Transfer-Encoding", "chunked") + + return c.SendStreamWriter(func(w *bufio.Writer) { + for { + fmt.Fprintf(w, "event: my-event\n") + fmt.Fprintf(w, "data: Hello SSE\n\n") + + if err := w.Flush(); err != nil { + log.Print("Client disconnected!") + return + } + } + }) +}) +``` + +You can find more details about this feature in [/docs/api/ctx.md](./api/ctx.md). + --- ## 🌎 Client package