# Waiting rooms for peak hours

> Leave a waiting room on in front of your busy pages: visitors walk straight in while there's room, a line forms only when the site is full, and checkout frees places at once.

Source: https://hibira.io/use-cases/waiting-room-traffic-peaks

Most busy minutes aren't announced. A newsletter lands in every inbox at once, a TV spot or a creator's post sends a crowd, a payday or lunch-hour rush doubles the usual traffic. Nobody planned a sale, so nobody turned on a waiting room, and the site slows down for everyone. An always-on waiting room sits in front of your busy pages every day: visitors walk in while you have room, and a line forms on its own only when the site is full.

## What your visitors see

- On a normal day: a moment's hop through the waiting page, which hands them a pass and sends them on.
- At a peak: their place in line and the estimated wait, updated live, instead of a slow or broken page.
- As soon as someone leaves, the next person in line goes in.

![Waiting rooms in the portal: live counts, the rate, when the line clears, and buttons to pause, let people in, mark sold out or post a notice.](https://hibira.io/portal/articles/waitroom-console-en-light.webp)

## What you get

- A limit in people, not requests: for example at most 2,000 people on the busy pages at once. Below that, everyone goes straight in.
- No schedule to remember: the room stays on, and the line forms and clears by itself.
- Places freed at checkout: send a leave after an order and the next person goes in at once.
- People waiting in line never count toward your app's realtime connections.
- A report after each peak: how many queued, how long they waited, minute by minute.

## Step 1: create an always-on room

```http
POST /apps/APP_ID/waiting_rooms/site
{
  "mode": "capacity",
  "max_active_users": 2000,
  "session_seconds": 600,
  "title": "Example Store",
  "return_urls": ["https://shop.example.com/"]
}
```

`max_active_users` is how many people your site serves comfortably at once. A visitor counts from the moment they're let in until `session_seconds` after their last heartbeat, or until they leave.

## Step 2: protect the busy pages

```js
// Cloudflare Worker in front of the busy pages
import { protect } from '@hibira-io/waiting-room/server';

const BUSY = ['/products', '/cart', '/checkout', '/search'];

export default {
  async fetch(request, env) {
    const { pathname } = new URL(request.url);
    if (BUSY.some((p) => pathname.startsWith(p))) {
      const blocked = await protect(request, { appKey: env.HIBIRA_KEY, secret: env.HIBIRA_SECRET, room: 'site' });
      if (blocked) return blocked;
    }
    return fetch(request);
  },
};
```

On Next.js, Express, Laravel, Django and other frameworks, use the middleware from [the waiting room guide](https://hibira.io/guides/waiting-room).

## Step 3: free places as people finish

```js
const base = 'https://ws.hibira.io/waitroom/APP_KEY/site';
const beat = () =>
  fetch(base + '/heartbeat', { method: 'POST', body: JSON.stringify({ pass }) })
    .then((r) => r.json())
    .then((j) => j.next_heartbeat_seconds && setTimeout(beat, j.next_heartbeat_seconds * 1000));
beat();

// after the order is placed
navigator.sendBeacon(base + '/leave', JSON.stringify({ pass }));
```

Without heartbeats, each visitor simply counts for one session after being let in.

## Tips

- Set `max_active_users` from a load test, or from what your site handled on its busiest normal day.
- Protect the pages that get heavy, such as product pages, cart, checkout and search. Leave the home page, images and static files outside.
- Every visitor who passes through the room counts as a queued visitor on your plan, so protecting only the busy pages also keeps the count down.
- Planned maintenance on the same site? Add a window and a warm-up to the same room: see [Waiting rooms for maintenance and reopenings](https://hibira.io/use-cases/waiting-room-maintenance).
