Wirebook

Wirebook › Bonus lesson

Does the app keep peeking, or does the website ring a doorbell?

Bonus lesson · about 6 minutes · hands-on · nothing you type leaves your browser

A brand-new lesson appears on a website. How does a news app on your phone find out? Does it keep peeking, again and again, or does the website ring a tiny doorbell? Race three messengers through a pretend day, count the wasted trips, then try being the peeker yourself.

The doorbell race

A handful of new lesson cards get published at random times today. Three messengers bring them to your app. Grey ticks are wasted trips (asked, nothing new). Coloured dots are deliveries, and the thin line before each dot shows how long the news waited.

Clock 24:00

Three ways to hear about something new

AHA! Peeking isn't silly: it's how most news readers and calendar apps still work, because it needs nothing special from the website. The trick is making each peek cheap.

Your turn: be the peeker

A pretend day zooms past in 36 seconds. New cards land in the letterbox, but you can't see them until you peek. Every peek is one trip. Get every card home with few wasted trips and short waits to earn three stars.

Clock 00:00

Peek at this website's real news feed

Wirebook has a real feed of new lessons at /feed.xml, the same kind of file news reader apps peek at. Press the button to fetch it from the server. Press it again: your browser shows the file's fingerprint (an ETag), so the server can answer with a tiny 304 Not Modified instead of sending the whole feed again. Nothing you type is sent; there's nothing to type.

(press the button)

So which is best?

It depends on who needs to be told and how fast. A chat app needs news in a second, so it uses push or keeps a connection open. A recipe blog's feed can be peeked at every hour by thousands of readers, and if each peek gets a 304, that costs almost nothing. Websites can also offer both: a feed for peekers, plus a hub that pings subscribed readers the moment the feed changes (one standard for this is called WebSub). Developers call the website-rings-another-server version a webhook: when your payment goes through or someone comments, one server calls a web address the other server gave it.

Doorbells need a bit of trust and setup: the website must keep a list of who to ring, and a doorbell address that leaked could let a stranger ring it too, so real webhooks are signed. Peeking needs no list at all, which is also why it's the private choice: the website doesn't learn who is waiting.

Peeking on a strict timetable is also how a game controller keeps the console up to date, hundreds of times a second: see how a game controller talks to a console.

Grown-up twin: Tooltern's help pages on 304 Not Modified and cron schedules (the timers that drive a lot of polling) show the same ideas with real headers.

Words from this lesson

Earn a bonus badge

Get all 3 right to collect the Peek or doorbell bonus badge. Bonus badges are extra: they don't change your stamp count or rank. Wrong answers are fine: just try again.

1. An app checks for news every minute, but news only arrives five times a day. What's the problem?

2. With push, what happens when a website publishes something new?

3. A feed reader peeks at a feed with the ETag it saved last time and gets 304. What does that mean?

Try another lesson

  1. 3Can a website get tired?7 min · interactive
  2. 12How does a game remember where you got to?6 min · interactive
  3. 1What happens when you open a web page?6 min · interactive