How-to
Website
Web app
Desktop / phone
Listen to server-sent events
A stream receives a one-way flow of events from a server — prices, progress, a log — over server-sent events. Use it when the server talks and the page only listens; for two-way messages, open a socket. This page shows the latest price as it arrives:
Needs: a server that sends events named price from /events.
page Prices(path: "/prices", title: "Prices", description: "Live prices.") {
state price = 0
stream ticks = sse("/events", events: ["price"]) {
receive Number
}
effect { if let p = ticks.last("price") { price = p } }
Heading("Prices").h1
match ticks {
open { Text("Price: {price}") }
else { Text("Connecting…").muted }
}
}
How it works
stream ticks = sse("/events", events: ["price"])opens the stream when the page is drawn and closes it when the reader leaves.events:names the event types to listen for; only those names are listened to.receive Numbersays what each event's data is, soticks.last(…)is aNumberand fitsprice. A stream that names nothing carriesJson, which is matched or decoded rather than read as a value.ticks.last("price")is the latest event of that name. Each event name is kept apart, so it is the last price and not the last event of any kind;ticks.last()is the last unnamed message. Theeffectcopies it intopriceeach time a new one arrives.match ticksreads the stream's state, with the same arms as a socket:connecting,open,closed(c),error(e), or anelse.
ticks.messages holds the messages in order — the last 500, or as many as keep: says. A message whose data parses as JSON arrives as a value.
The browser reconnects a stream on its own and resumes from the last event id the server sent. Pass credentials: true to send the site's cookies with the request.