# Start with a script tag

> A multiplayer page in one HTML file, with no build step, no CLI and no server code.

A multiplayer page can be one HTML file: a meta tag naming your
project, and a module script that imports the client from `irt.io` and
connects to a room. There's nothing to install, build or deploy, and no
room code to write.

## 1. Create a project

Sign in at `app.irt.io` and create a project. Its slug, such as `dots`,
names it everywhere: 3 to 40 lowercase letters, digits and single
hyphens, starting with a letter, unique across IRT. The project's URL
is `https://rooms.irt.io/dots/`, and each of its rooms has a URL under
it, such as `https://rooms.irt.io/dots/lobby`.

A new project has no build yet, so its rooms are of one room type,
`default`, which is a [native room](/docs/guides/concepts#rooms) with no
schema. A native room runs none of your code: it sends each message on
to the other players in the room, and keeps
[presence](/docs/room/presence), who's in the room, and a leader. The
project's settings let players join as guests and create a room by
connecting to it, so nothing else needs setting up.

## 2. Write the page

Every player is a dot that follows their pointer, in their own colour:

```html
<!doctype html>
<meta charset="utf-8">
<meta name="irt-project" content="https://rooms.irt.io/dots/">
<title>Dots</title>
<p id="status">Connecting</p>
<canvas width="600" height="400" style="border: 1px solid"></canvas>
<script type="module">
  import { connect } from "https://irt.io/client/0.js";

  const canvas = document.querySelector("canvas");
  const ctx = canvas.getContext("2d");
  const status = document.querySelector("#status");
  const dots = new Map();

  const room = await connect(new URLSearchParams(location.search).get("room") ?? "lobby");
  room.presence.setStatus({ hue: Math.floor(Math.random() * 360) });
  showPlayers();

  room.addEventListener("message", (e) => dots.set(e.from, e.data));
  room.presence.onJoin(showPlayers);
  room.presence.onLeave((p) => {
    dots.delete(p.player);
    showPlayers();
  });
  room.presence.onLeader(showPlayers);
  room.addEventListener("reconnecting", () => (status.textContent = "Reconnecting"));
  room.addEventListener("open", showPlayers);
  room.addEventListener("close", (e) => (status.textContent = `Closed: ${e.error?.message ?? "you left"}`));

  canvas.addEventListener("pointermove", (e) => {
    const me = { x: e.offsetX, y: e.offsetY };
    dots.set(room.player, me);
    room.send(me, { unreliable: true });
  });

  function showPlayers() {
    const leading = room.presence.leader === room.player ? ", and you lead" : "";
    status.textContent = `${room.presence.size} here${leading}`;
  }

  function draw() {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for (const [player, { x, y }] of dots) {
      ctx.fillStyle = `hsl(${room.presence.get(player)?.status?.hue ?? 0} 70% 50%)`;
      ctx.beginPath();
      ctx.arc(x, y, player === room.player ? 8 : 6, 0, Math.PI * 2);
      ctx.fill();
    }
    requestAnimationFrame(draw);
  }
  draw();
</script>
```

Replace `dots` in the meta tag with your project's slug. Then open the
page from any web server, such as `python3 -m http.server` in its
folder, in two browsers, and move the pointer in each. A module script
doesn't load from a `file:` URL, so the page needs a server, but any
will do: see [Hosting your page](/docs/hosting/pages) for putting it online.

## What the page does

**The meta tag** declares the project's URL to the page, the way a
`<base>` tag declares its base URL. `connect("lobby")` resolves `lobby`
against it, as `new URL("lobby", "https://rooms.irt.io/dots/")` would,
and connects to `https://rooms.irt.io/dots/lobby`. Without the tag,
pass the absolute room URL instead; a relative one throws a
`TypeError`.

**The room** is created by the first player to connect to its URL, and
hibernates 30 seconds after the last one closes. Each `?room=` value is
its own room, so `index.html?room=forest-7` is a second board.

**`await connect(...)`** resolves once the client is in the room and
holds its state, so `room.player`, the player's ID, and `room.presence`
are set. Without a player token, the player is a guest, kept in the
browser's `localStorage`, so every tab of one browser is the same
player: a second tab closes the first's connection. Test with two
browsers, a private window, or `connect(url, { guest: "tab" })`, which
makes each tab its own guest.

**Messages** go from the sender to every other player in the room, as
JSON, and arrive as `message` events whose `data` is the message and
whose `from` is the sender's player ID. `{ unreliable: true }` sends
the pointer as an unreliable message, which may be dropped or arrive
out of order: right for a position, since a newer one follows at once.
Leave it out for anything that must arrive, such as a chat line.

**Presence** lists the players in the room. Each sets its own status,
here its colour, of at most 64 bytes of JSON in a native room, and
every client sees it. A player whose connection drops stays in presence
for 10 seconds, so a reconnect doesn't look like a leave; `onLeave` runs
when they're gone. The room's **leader** is the player who has been
connected longest, and passes the role on with `room.lead(player)`. Use
it for whatever one player should decide, such as when a round starts.

**Events** follow the `WebSocket` pattern: the client is an
`EventTarget`, and its `readyState` is `"connecting"`, `"waking"`,
`"open"`, `"reconnecting"` or `"closed"`. The client reconnects by
itself after a dropped connection, firing `reconnecting` and then
`open`; `close` fires only when the connection ends for good, with the
reason in `e.error`. `room.close()` leaves on purpose.

## Versions

`https://irt.io/client/0.js` is the latest 0.x version of the client,
the same code as `@irtio/client`, so a page gets fixes without
changing. Name an exact version to pin it, such as
`https://irt.io/client/0.1.0.js`. Until IRT's first release, an exact
version's contents can still change, and every file here is cached for
at most five minutes. The [UI kit](/docs/client/ui-kit) is at
`https://irt.io/client/0/ui.js` and voice at
`https://irt.io/client/0/voice.js`.

## Growing it

A native room trusts its players: any of them can send anything. When a
game needs rules the room enforces, state that outlasts a visit, or
typed calls, add room code. The [Quickstart](/docs/guides/quickstart)
writes the same dots game with a schema, room code and tests, and
deploys it as a build with the `irt` CLI. The page above keeps working
as it is: a build whose `default` room type is native,
`rooms: { default: { native: true } }` in `irt.config.ts`, serves it
unchanged, and a native room with a [schema](/docs/room/ownership)
adds synced entities that players spawn, own and write.

- [Concepts](/docs/guides/concepts) defines the words these docs use.
- [Hosting your page](/docs/hosting/pages) covers itch.io, GitHub Pages, your
  own domain and IRT's sites.
- [Client SDK](/docs/client/sdk) is the client's reference.
