# quickJoin() function

> The quickJoin() function finds or creates a public room with space and resolves with its ID, URL and join code.

The **`quickJoin()`** function finds a public room with space, or creates one, and resolves with its ID, URL and join code, for games that fill rooms with whoever comes. Pass the URL to [`connect()`](/docs/client/connect).

## Syntax

```ts
quickJoin()
quickJoin(init)
```

### Parameters

`init` Optional
: An object with any of these properties:

  `project`
  : A string or a `URL`: the project URL, such as `"https://rooms.irt.io/my-game/"`. Defaults to the page's `irt-project` meta tag.

  `token`
  : A string: a player token. Guests leave it out.

  `signal`
  : An `AbortSignal` that aborts the request.

  `type`
  : A string: the [room type](/docs/guides/concepts#rooms) to pick or create. Defaults to the type named `default`.

  `meta`
  : An object. Only rooms whose `meta` has every one of its top-level keys, with an equal value, are picked, and a new room gets it as its `meta`.

  `maxPlayers`
  : A number: a new room's `maxPlayers`, from 1 to the room size's connection limit.

### Return value

A `Promise` that resolves with an object with these properties:

`id`
: The room's ID.

`url`
: The room's URL, absolute, to pass to `connect()`.

`joinCode`
: The room's join code, or `null` if it has none.

### Exceptions

`TypeError`
: Rejects with this if there's no project URL, no `project` given and no `irt-project` meta tag; if the project URL's path doesn't end in a project; or if it isn't an `http:`, `https:`, `ws:` or `wss:` URL.

[`IrtError`](/docs/client/irt-error)
: Rejects with this if the router refuses the request, with the HTTP status as its `code`: 403, a `NotAllowedError`, when the project's `players.quickJoin` is off; 400 for `meta` over 1 KB, a `maxPlayers` out of range or no `type` when the build has no type named `default`; 401 for an invalid token; and 429, a `QuotaExceededError`, when the address is rate limited or the project has `limits.maxRooms` rooms. If the request didn't complete, its `code` is 0 and its `name` is `TimeoutError` when `signal` came from `AbortSignal.timeout()` and aborted, `AbortError` when another `signal` aborted, with its reason as `cause`, and `NetworkError` otherwise, with the failed `fetch()`'s error as `cause`.

## Description

`quickJoin()` picks the active public room with the most players that's under its connection limit and its own `maxPlayers`, among those whose `meta` matches. Without one, it picks such a hibernated room, and without that, it creates a public room with `meta` as its `meta` and `maxPlayers` as its `maxPlayers`.

It holds the room's slot for 10 seconds or until the client connects, so players who quick join at once don't overfill a room or each create one. A room whose code called [`room.setPublic(false)`](/docs/room/room/setpublic), such as a game whose seats are taken, isn't picked, active or hibernated, until it sets it back.

The project's `players.quickJoin` setting is off by default, since a game whose rooms aren't open to anyone mustn't let strangers in. Matchmaking by skill, parties and queues belong in your backend.

## Examples

### Playing a mode with one button

The play button puts the player in a room of the mode they picked, and the same signal cancels both steps.

```ts
// play-button.ts
import { connect, IrtError, quickJoin } from "@irtio/client";

export async function play(mode: "duel" | "squad", signal: AbortSignal) {
  try {
    const { url } = await quickJoin({ meta: { mode }, maxPlayers: mode === "duel" ? 2 : 8, signal });
    return await connect(url, { signal });
  } catch (e) {
    if (e instanceof IrtError && e.name === "AbortError") return undefined;
    throw e;
  }
}
```

`maxPlayers` applies only if `quickJoin()` creates the room. A room it finds keeps its own.

## See also

- [`listPublic()`](/docs/client/listpublic)
- [`createRoom()`](/docs/client/createroom)
- [`Room.setPublic()`](/docs/room/room/setpublic)
- [Games without a backend](/docs/guides/no-backend#creating-and-joining-rooms)
- [Errors and close codes](/docs/client/errors#room-wide-requests)
