Skip to main content

2 posts tagged with "sse"

View All Tags

Live HTML Over SSE: Pushing Rendered Fragments With app.Render in Fiber v3.6

ยท 29 min read
Fiber Team
Maintainers

A support team works from a shared queue page. Every agent has it open all day, and the page polls the server every five seconds. Two things go wrong with that. The queue is never quite current, so two agents regularly click "Claim" on the same ticket within the same five seconds, and one of them finds out a few seconds later that their work was wasted. And the polling is mostly empty: thirty open tabs ask the server 360 times a minute whether anything happened, and the answer is nearly always no.

Server-Sent Events fix both. The server keeps one response open per tab and writes to it when something changes. The question is what to write. Sending JSON means a second rendering layer in the browser that has to produce the same markup as your Go templates. Sending HTML means you render the fragment on the server with the same template that renders the full page, and the browser only has to put it in the right place. With htmx and its SSE extension, that last part needs no JavaScript of your own.

Rendering a template used to be tied to a request in Fiber: c.Render writes into the response of the request at hand. An update that one agent triggers has to reach thirty other streams, and none of them is the request that caused it. Fiber v3.6.0 added app.Render, which writes a template through the configured view engine into any io.Writer, with no request involved. This post builds the live queue around it: render each update once, number it, fan it out to every subscriber, replay what a reconnecting browser missed and send a full reset when replay is impossible. Everything was built and tested against Fiber v3.6.1 with html/v3.1.0 of the template engine, htmx 2.0.11 and htmx-ext-sse 2.2.4. app.Render needs Fiber v3.6.0 or later, which requires Go 1.26. The SSE middleware has been there since v3.3.0.

Nobody Cancels Your Handler: Timeouts and Client Disconnects in Fiber v3

ยท 17 min read
Fiber Team
Maintainers

Picture a bug report that is really a cost problem. A summary endpoint calls a slow upstream, an LLM API in our case, and users who get bored close the tab after a second or two. The frontend is fine with that. The invoice is not: the upstream bill shows complete generations for requests whose clients were long gone. The handler passed its context to the upstream client, so everybody assumed that a closed connection would cancel the call. It never did.

If you came to Fiber from net/http, Gin, Echo or Chi, that assumption is reasonable. In those frameworks, the request context is canceled when the client disconnects. Fiber runs on fasthttp, and fasthttp does not watch the connection while your handler runs. The fiber.Ctx you get satisfies context.Context, so the code compiles, but it is a context that can never be canceled. It is one of the most frequently reported surprises in the issue tracker (see #4335 or #4263), and the method docs in v3.5.0 now state it in plain words.

This post shows what you can rely on instead, with one service that we build and test against Fiber v3.5.0: a deadline for request/response handlers, disconnect detection for streams, and a clean way to end long streams when the server shuts down.