var errDone = errors.New("done")
func handler(ctx iris.Context) {
ctx.ContentType("text/html")
ctx.Header("Transfer-Encoding", "chunked")
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
// Send the response in chunks and
// wait for half a second between each chunk,
// until connection closed.
err := ctx.StreamWriter(func(w io.Writer) error {
ctx.Writef("Message number %d<br>", ints[i])
time.Sleep(500 * time.Millisecond) // simulate delay.
return errDone // ends the loop.
return nil // continue write
// Test it by canceling the request before the stream ends:
// [ERRO] $DATETIME stream: context canceled.
ctx.Application().Logger().Errorf("stream: %v", err)