Skip to content

Commit

Permalink
Add docs from gofiber/fiber@31a503f
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 27, 2024
1 parent a448f33 commit aa96037
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
60 changes: 60 additions & 0 deletions docs/core/api/ctx.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
38 changes: 38 additions & 0 deletions docs/core/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ DRAFT section
- Reset
- Schema -> ExpressJs like
- SendStream -> ExpressJs like
- SendStreamWriter
- SendString -> ExpressJs like
- String -> ExpressJs like
- ViewBind -> instead of Bind
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aa96037

Please sign in to comment.