Skip to content

Commit

Permalink
📚 Doc: Add SendStreamWriter to docs/api/ctx.md
Browse files Browse the repository at this point in the history
  • Loading branch information
grivera64 committed Sep 14, 2024
1 parent c977b38 commit 024ac5e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/api/ctx.md
Original file line number Diff line number Diff line change
Expand Up @@ -1871,6 +1871,47 @@ 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 flush data before the function returns, you can call `w.Flush()`
on the provided writer. Otherwise, the buffered stream flushes after
`streamWriter` returns.
:::

```go title="Example"
app.Get("/wait", func(c fiber.Ctx) error {
return c.SendStreamWriter(func(w *bufio.Writer) {
fmt.Fprintf(w, "Waiting for 10 seconds\n")
if err := w.Flush(); err != nil {
log.Print("User quit early")
}
time.Sleep(10 * time.Second)
fmt.Fprintf(w, "Done!\n")
})
})
```

## Set

Sets the response’s HTTP header field to the specified `key`, `value`.
Expand Down

0 comments on commit 024ac5e

Please sign in to comment.