[API reference](https://jsprismarine.dev/api/index.md) / [@jsprismarine/prismarine](https://jsprismarine.dev/api/@jsprismarine/prismarine/index.md) / network/transport/Transport

# network/transport/Transport

## Interfaces

### Transport

Defined in: [packages/prismarine/src/network/transport/Transport.ts:90](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L90)

A listening transport: accepts connections and surfaces what arrives on them.

Subscriptions rather than an event-map emitter, so each listener keeps its own signature.
Listeners of one subscription run in order, each awaited before the next. A transport
delivers one connection's connect, frames and disconnect in order, none overtaking another.

#### Type Parameters

##### C

`C` *extends* [`TransportConnection`](#transportconnection) = [`TransportConnection`](#transportconnection)

The transport's own connection type, for a consumer that needs more than
the [TransportConnection](#transportconnection) contract.

#### Properties

##### connectionIds?

> `readonly` `optional` **connectionIds?**: `ConnectionIds`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:112](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L112)

The allocator the transport numbers its connections from. A transport handed to the server
in place of its own sockets gives it so the Java Edition listener, which `java-enabled`
starts beside it, numbers its connections from the same one.

#### Methods

##### close()

> **close**(): `void`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:106](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L106)

Stops listening and drops every connection.

###### Returns

`void`

##### onConnect()

> **onConnect**(`listener`): `void`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:92](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L92)

Calls the listener once per accepted connection.

###### Parameters

###### listener

(`connection`) => `void` \| `Promise`\<`void`\>

###### Returns

`void`

##### onDisconnect()

> **onDisconnect**(`listener`): `void`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:97](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L97)

Calls the listener when the transport forgets a connection, however it ended. It also runs
for a connection whose connect never reached [Transport.onConnect](#onconnect) listeners.

###### Parameters

###### listener

(`id`, `address`, `reason`) => `void` \| `Promise`\<`void`\>

###### Returns

`void`

##### onFrame()

> **onFrame**(`listener`): `void`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:99](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L99)

Calls the listener with each frame a connected client sent, and the connection's ID.

###### Parameters

###### listener

(`payload`, `id`) => `void` \| `Promise`\<`void`\>

###### Returns

`void`

##### onRaw()?

> `optional` **onRaw**(`listener`): `void`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:104](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L104)

Calls the listener with datagrams from hosts that are not connected, such as queries.
Only a transport that shares its socket with such traffic implements it.

###### Parameters

###### listener

(`payload`, `address`) => `void` \| `Promise`\<`void`\>

###### Returns

`void`

***

### TransportCapabilities

Defined in: [packages/prismarine/src/network/transport/Transport.ts:14](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L14)

What a transport does for the session layer, so the layers above can decide what they
still have to do themselves: batch, compress, encrypt, or re-order.

#### Properties

##### encrypted

> `readonly` **encrypted**: `boolean`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:16](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L16)

Whether the transport encrypts frames itself, as a WebRTC data channel does.

##### maxFrameSize

> `readonly` **maxFrameSize**: `number`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:20](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L20)

The most payload bytes one [TransportConnection.sendFrame](#sendframe) call may carry.

##### reliableOrdered

> `readonly` **reliableOrdered**: `boolean`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:18](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L18)

Whether frames arrive exactly once and in the order they were framed.

***

### TransportConnection

Defined in: [packages/prismarine/src/network/transport/Transport.ts:32](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L32)

One client's duplex byte-frame channel. It knows nothing about the packets it carries: a
frame is an opaque payload, already batched, compressed, and encrypted by the layers above as
[TransportConnection.capabilities](#capabilities) requires.

A Bedrock frame is a batch in the form RakNet carries it, starting with the `0xFE` batch id. A
transport whose wire leaves that byte out, as NetherNet's does, adds it to what it receives and
strips it from what it sends, so the batch and encryption codecs above see one format.

#### Properties

##### address

> `readonly` **address**: [`InetAddress`](https://jsprismarine.dev/api/@jsprismarine/raknet/classes/InetAddress.md)

Defined in: [packages/prismarine/src/network/transport/Transport.ts:43](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L43)

Where the client connected from: an IP address and port, whichever transport carried it.
`InetAddress` is the value type for that, not a RakNet dependency of the seam. Several
connections may share one, and an address may come back on a later connection.

##### capabilities

> `readonly` **capabilities**: [`TransportCapabilities`](#transportcapabilities)

Defined in: [packages/prismarine/src/network/transport/Transport.ts:45](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L45)

What this connection's transport does on its own - see [TransportCapabilities](#transportcapabilities).

##### id

> `readonly` **id**: `number`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:37](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L37)

The server's ID for this connection, drawn from the allocator every transport of the
server shares, so no two live or past connections have the same one.

##### transport

> `readonly` **transport**: [`TransportName`](#transportname)

Defined in: [packages/prismarine/src/network/transport/Transport.ts:47](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L47)

The transport this connection arrived over.

#### Methods

##### close()

> **close**(`reason?`): `void` \| `Promise`\<`void`\>

Defined in: [packages/prismarine/src/network/transport/Transport.ts:63](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L63)

Hangs up on the client, after what was already sent has had its chance to arrive.

RakNet holds the session open until the client acknowledges its last datagrams, so it
hangs up at once. A transport that would drop frames still in flight waits for them
first and returns a promise that settles once it has hung up, so a caller that sent a
goodbye can wait for it to arrive before the process exits.

###### Parameters

###### reason?

`string`

Recorded as the disconnect reason the transport reports back.

###### Returns

`void` \| `Promise`\<`void`\>

##### provesKey()?

> `optional` **provesKey**(`identityPublicKey`): `Promise`\<`boolean` \| `undefined`\>

Defined in: [packages/prismarine/src/network/transport/Transport.ts:73](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L73)

Whether the client proved, while connecting, that it holds a key. Only a transport that
encrypts the wire itself has this: there the session leaves Minecraft's encryption off, and
with it the handshake that proves a login's identity key, so the transport's own handshake
has to prove it, or a login somebody recorded could be sent again by anybody.

###### Parameters

###### identityPublicKey

`string`

The key a login names, as base64 DER.

###### Returns

`Promise`\<`boolean` \| `undefined`\>

False when the client proved another key or none. Undefined when the way this
  client connected gives it no means to prove a key, as NetherNet's LAN discovery does not.

##### roundTripTime()?

> `optional` **roundTripTime**(): `number` \| `undefined`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:78](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L78)

The round trip time to the client in milliseconds, as the transport measures it, or
undefined before its first sample. A transport that measures none leaves it out.

###### Returns

`number` \| `undefined`

##### sendFrame()

> **sendFrame**(`payload`, `immediate?`): `void`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:53](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L53)

Queues one frame for the client.

###### Parameters

###### payload

`Buffer`

The frame's bytes, ready for the wire.

###### immediate?

`boolean`

Send in a datagram of its own instead of the next scheduled flush.

###### Returns

`void`

## Type Aliases

### TransportName

> **TransportName** = *typeof* [`TransportNames`](#transportnames)\[keyof *typeof* [`TransportNames`](#transportnames)\]

Defined in: [packages/prismarine/src/network/transport/Transport.ts:8](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L8)

One of [TransportNames](#transportnames): `RakNet`, `NetherNet`, or `Java` for Java Edition over TCP.

## Variables

### TransportNames

> `const` **TransportNames**: `object`

Defined in: [packages/prismarine/src/network/transport/Transport.ts:5](https://github.com/JSPrismarine/JSPrismarine/blob/e928b08f56694af6821b757ce6b196faebd3199c/packages/prismarine/src/network/transport/Transport.ts#L5)

The display name of each transport a connection can arrive over, keyed by a lowercase id.

#### Type Declaration

##### java

> `readonly` **java**: `"Java"` = `'Java'`

##### nethernet

> `readonly` **nethernet**: `"NetherNet"` = `'NetherNet'`

##### raknet

> `readonly` **raknet**: `"RakNet"` = `'RakNet'`
