# Connection

> The Connection interface of the Room API is one open connection from a player's client to the room.

The **`Connection`** interface of the [Room API](/docs/room) is one open connection from a player's client to the room. The hooks about one connection, such as [`onJoin()`](/docs/room/room-definition/onjoin), [`onMessage()`](/docs/room/room-definition/onmessage) and [`onLeave()`](/docs/room/room-definition/onleave), receive it, and [`room.connections`](/docs/room/room/connections) holds every open one by its ID.

Room code never constructs a `Connection`. The room host makes one when a connection opens, and passes the same object to every hook about that connection until it closes.

## One connection per player

A player has at most one open connection to a room. When a player connects again, from a new tab or another device, the older connection closes with close code 4012, and `onLeave` runs for it before `onJoin` runs for the new one. The older client doesn't reconnect by itself. The player's [presence](/docs/room/presence) entry stays as it was, so other players don't see a leave and a join.

So a connection stands for a player only while it's open. Keep what belongs to the player, such as private state, owned entities and bans, by [`player`](/docs/room/connection/player), not by connection.

## Instance properties

[`Connection.id`](/docs/room/connection/id)
: The connection's ID, its key in `room.connections`. Read only.

[`Connection.player`](/docs/room/connection/player)
: The player ID of the player who opened it. Read only.

[`Connection.meta`](/docs/room/connection/meta)
: The `meta` claim of the connection's token, or `null`. Read only.

[`Connection.data`](/docs/room/connection/data)
: An object for the room's own use, such as the connection's player entity. It isn't sent to clients.

[`Connection.viewTick`](/docs/room/connection/viewtick)
: The tick of the room state the player was looking at when it sent the input applied this tick. Read only.

[`Connection.input`](/docs/room/connection/input)
: The input applied this tick, in the stepper and `onTick`. Read only.

[`Connection.rtt`](/docs/room/connection/rtt)
: The client's lowest recent round trip to the room in milliseconds, as the client reports it. Read only.

[`Connection.protocol`](/docs/room/connection/protocol)
: The client protocol the connection speaks. Read only.

## Examples

### Keeping a player's ship on the connection

```ts
// ships.ts
import { defineRoom, defineSchema, entity, type RoomEntity } from "@irtio/room";

const Ship = entity("ship", { pilot: "string", heading: "f32" }, { key: "pilot" });

export default defineRoom({
  schema: defineSchema({ entities: [Ship] }),
  onJoin(room, connection) {
    connection.data.ship = room.entities.get(Ship, connection.player) ?? room.entities.create(Ship, { pilot: connection.player });
  },
  onMessage(room, connection, message) {
    const ship: RoomEntity<typeof Ship> = connection.data.ship;
    if (typeof message === "number") ship.heading = message;
  },
});
```

## See also

- [`Room`](/docs/room/room)
- [Presence](/docs/room/guides/presence), for who is in the room rather than which connections are open
- [Room SDK](/docs/room/sdk#room), the whole of `@irtio/room` on one page
