# Join tickets

> Let your server decide who may join which room, and tell jsflow who each peer is.

By default anyone who has your publishable key and a room's name can join that room from an allowed origin. A **join ticket** closes that: your server signs a short-lived ticket for one user and one room, and jsflow admits only joins that carry one.

## What a ticket is

An HS256 JWT, signed by your backend with the app's **secret key** (`sk_…`). jsflow verifies it and never calls you.

- Header: `{"alg": "HS256", "typ": "JWT", "kid": "<the secret key's id>"}`. The `kid` lets two keys be active while you rotate.
- Claims: `app`, `room`, `sub` (your user's id: 1 to 256 **bytes** of UTF-8, not characters), `iat`, `exp` (at most 10 minutes ahead; 60 seconds of clock skew is allowed), and optionally `region`.
- Signature: HMAC-SHA-256 keyed with the bytes of the full `sk_…` string.

jsflow checks the signature, the expiry, the app, the room and the app's blocklist. The `sub` you sign becomes `room.user(peerId)` for every peer in the room, so peers can tell who is who without trusting each other.

**A ticket can be used again until it expires.** jsflow does not remember tickets, so anyone who holds one can join that room as that user until its `exp`. Keep the lifetime short (the examples use 300 seconds, the most is 10 minutes) and sign a new ticket for every join.

## On your server

Sign a ticket when a signed-in user asks to enter a room, after you have decided that they may. Complete, dependency-free versions for Node, Python and Go are in the [`examples/tickets/` folder of the jsflow-community repository](https://github.com/precisit/jsflow-community/tree/main/examples/tickets); each is one function and a small endpoint. The user id must come from your own session, never from the request.

## In the page

Pass a function, so that every `join()` call gets a fresh ticket (a reload, or your own rejoin after `leave()`). The SDK calls it once per `join()`: a return from `away` after a sleep or a network change, and a token refresh, keep the peer's identity and need no ticket.

```js title="client.js"
const room = await join({
  app: 'pk_…',
  room: 'team-7',
  ticket: async () => {
    const res = await fetch('/ticket', {
      method: 'POST',
      body: JSON.stringify({ room: 'team-7' }),
    });
    return (await res.json()).ticket;
  },
});
```

## Turning it on

In the dashboard, open the app and switch on **Require join tickets**. This needs an active secret key. From then on a join without a valid ticket is refused with `ticket_required`, `ticket_invalid` or `ticket_expired`.

## Keys and rotation

Keep the secret key in an environment variable or your secrets store. An app can have two active secret keys: create the new one, deploy it, then revoke the old one. Tickets signed with a revoked key stop being accepted within about 30 seconds.

Source: https://jsflow.com/docs/guides/join-tickets/
