# Quick start

> Join a room from a plain HTML page and send your first messages.

You need an app key and a page served from an origin the app allows.

The **demo app** works on `http://localhost` and `http://127.0.0.1` (any port), allows up to 16 peers a room, and is shared by everyone trying jsflow. Its key is `pk_O6puVNC7RYNarF7gt1YYQ3dDccBiX90denbw`. For your own app, with your own origins and limits, create one in the dashboard; it gives you a key and this same snippet with the key filled in.

## The page

Save this as `index.html`:

```html check title="index.html"
<script type="module">
  const { join, text } = await import(
    'https://cdn.jsflow.com/v1.1.3/jsflow.js'
  );

  let room;
  try {
    // The demo key. The demo app is shared, so pick a room name of
    // your own.
    room = await join({
      app: 'pk_O6puVNC7RYNarF7gt1YYQ3dDccBiX90denbw',
      room: 'try-k3Hq9xZ2',
    });
  } catch (e) {
    // A JoinError: its code says why, for example 'origin' (see
    // Troubleshooting).
    console.error('could not join', e);
    throw e;
  }
  console.log('my id', room.id);

  // 'joining' → 'connected'
  room.on('state', (s) => console.log('state', s));
  room.on('message', (from, data, info) => {
    console.log(from, text(data), info);
  });
  room.on('peer', (id, up) => {
    console.log('peer', id, up ? 'here' : 'gone');
    if (up) room.send(id, 'hi, ' + id);   // to one peer
  });
  // To everyone in the room, once a second.
  setInterval(() => room.broadcast('tick ' + Date.now()), 1000);
</script>
```

> **What to expect on a first run.** The first tab joins and then waits: its console shows `state joining`, and nothing more happens until another peer arrives. Open a second tab or window on the same page, in the same room, to see the connection: both consoles then show `peer <id> here`, and each receives the other's `hi` and `tick` messages. If the join fails with `could not join` and the `JoinError` code `origin`, check the exact scheme, host and port of the address in your browser: `http://localhost:8080` and `http://127.0.0.1:8080` are different origins (the demo app allows both).

## Run it

Serve the page over HTTP from localhost, in its folder:

```sh
python3 -m http.server 8080
```

No Python? `npx serve -l 8080` does the same. Open `http://localhost:8080/` in two windows side by side, or in two browsers, with the developer console open. In the first window the console shows `state joining` and then, within a couple of seconds of the second window opening, `peer <id> here` and the other window's `hi` and `tick` messages; the second window shows the same. A page opened from `file://` has no origin the service accepts, so it must come from a server. If the console shows `could not join` with a `JoinError` whose `code` is `origin`, the address in the browser is not one the app allows: `http://localhost:8080` and `http://127.0.0.1:8080` count as different origins, and the demo app allows both. Every other code is in [Troubleshooting](https://jsflow.com/docs/guides/troubleshooting.md). The browser must be Chrome or Edge 137, Safari 17 or Firefox 130 or newer: an older one cannot join (it gets `unsupported_browser`).

In Chrome, two tabs work too. Safari and phones take a background tab out of the room until it is shown again (see [lifecycle](https://jsflow.com/docs/concepts/lifecycle.md)).

## From npm

```sh
npm install jsflow-sdk
```

```ts check title="app.ts"
import { join, text } from 'jsflow-sdk';

const room = await join({ app: 'pk_…', room: 'lobby' });
room.on('message', (from, data) => console.log(from, text(data)));
room.broadcast('hello');
```

Vite and webpack bundle the SDK and its worker for you. [Loading the SDK](https://jsflow.com/docs/guides/loading-the-sdk.md) covers the CDN, other bundlers, a pinned version with an integrity hash, and the Content-Security-Policy.

## Attach listeners right after `join()`

Attach your listeners **right after `await join()`**, before awaiting anything else. No event is lost then: events reach the page as separate tasks, after your code that follows the `await` has run. If you await something else first, early `peer` and `message` events can pass with no listener.

## Next

- [Rooms, peers and keys](https://jsflow.com/docs/concepts/rooms-peers-keys.md): what the words mean.
- [Choosing a state model](https://jsflow.com/docs/concepts/choosing-a-state-model.md): who decides, who sends the state to a late joiner. Building a game? [For games](https://jsflow.com/docs/guides/for-games.md).
- [Choosing how to send](https://jsflow.com/docs/concepts/choosing-how-to-send.md): `broadcast`, `send` or `sendReliable`.
- [Loading the SDK](https://jsflow.com/docs/guides/loading-the-sdk.md): bundlers, TypeScript, CSP.

Source: https://jsflow.com/docs/start/quick-start/
