Skip to content

TcpListener

Defined in: net-transport-node/src/tcp-listener.ts:84

Accepts TCP connections, cleartext or TLS, and yields each as a TransportConnection over a DuplexTransport. Built on @ac-kit/node’s TcpServer and TlsServer.

Accepted sockets allow half-open connections: the peer’s end arrives as the transport’s end, and the local side keeps writing until it ends or destroys the transport itself. A TLS handshake failure is reported as error; the listener carries on.

const listener = await TcpListener.listen({ host: "127.0.0.1", port: 0 });
listener.subscribe("connection", ({ transport }) => serve(transport));
```;
## Extends
- `EventDispatcherMapBase`\<`ListenerEvents`\>
## Implements
- `IListener`
## Properties
### local
> `readonly` **local**: `TransportEndpoint`
Defined in: net-transport-node/src/tcp-listener.ts:92
#### Implementation of
`IListener.local`
## Methods
### \[asyncDispose\]()
> **\[asyncDispose\]**(): `Promise`\<`void`\>
Defined in: net-transport-node/src/tcp-listener.ts:148
Closes the listener, so it can be used with `await using`.
#### Returns
`Promise`\<`void`\>
***
### close()
> **close**(): `Promise`\<`void`\>
Defined in: net-transport-node/src/tcp-listener.ts:137
Stop accepting connections.
Already-accepted connections are not affected: they belong to whoever
received them. Idempotent.
#### Returns
`Promise`\<`void`\>
Resolves once the listener released its address.
#### Implementation of
`IListener.close`
***
### isSubscribed()
> **isSubscribed**\<`K`\>(`event`, `listener`): `boolean`
Defined in: async/src/events/event-dispatcher-map-base.ts:38
Check if a callback is subscribed to a specific event.
#### Type Parameters
##### K
`K` *extends* keyof `ListenerEvents`
#### Parameters
##### event
`K`
The event name.
##### listener
`Callable`\<`ListenerEvents`\[`K`\]\>
The callback to check.
#### Returns
`boolean`
#### Implementation of
`IListener.isSubscribed`
#### Inherited from
`EventDispatcherMapBase.isSubscribed`
***
### subscribe()
> **subscribe**\<`K`\>(`event`, `listener`, `options?`): `Callable` \| `null`
Defined in: async/src/events/event-dispatcher-map-base.ts:23
#### Type Parameters
##### K
`K` *extends* keyof `ListenerEvents`
#### Parameters
##### event
`K`
##### listener
`Callable`\<`ListenerEvents`\[`K`\]\>
##### options?
`EventDispatcherSubscribeOptions`
#### Returns
`Callable` \| `null`
#### Implementation of
`IListener.subscribe`
#### Inherited from
`EventDispatcherMapBase.subscribe`
***
### unsubscribe()
> **unsubscribe**\<`K`\>(`event`, `listener`): `void`
Defined in: async/src/events/event-dispatcher-map-base.ts:31
Unsubscribe a callback from a specific event.
#### Type Parameters
##### K
`K` *extends* keyof `ListenerEvents`
#### Parameters
##### event
`K`
The event name.
##### listener
`Callable`\<`ListenerEvents`\[`K`\]\>
The callback to remove.
#### Returns
`void`
#### Implementation of
`IListener.unsubscribe`
#### Inherited from
`EventDispatcherMapBase.unsubscribe`
***
### wait()
> **wait**\<`K`\>(`event`, `options?`): `Promise`\<`ListenerEvents`\[`K`\]\>
Defined in: async/src/events/event-dispatcher-map-base.ts:45
Wait for the next occurrence of a specific event, optionally matching a
predicate.
This is a convenience method that subscribes to the dispatcher, waits for
the next event that matches the predicate (if provided), and then
unsubscribes automatically.
#### Type Parameters
##### K
`K` *extends* keyof `ListenerEvents`
#### Parameters
##### event
`K`
The event name to wait for.
##### options?
`EventDispatcherWaitOptions`\<`ListenerEvents`\[`K`\]\>
Optional settings including predicate and abort signal.
#### Returns
`Promise`\<`ListenerEvents`\[`K`\]\>
A promise that resolves with the event arguments as an array.
#### Implementation of
`IListener.wait`
#### Inherited from
`EventDispatcherMapBase.wait`
***
### listen()
> `static` **listen**(`options`): `Promise`\<`TcpListener`\>
Defined in: net-transport-node/src/tcp-listener.ts:119
Bind and start accepting.
#### Parameters
##### options
[`TcpListenOptions`](../type-aliases/TcpListenOptions.md)
Address to bind, and accept options.
#### Returns
`Promise`\<`TcpListener`\>
The listener, once bound.
#### Throws
When the address cannot be bound (in use, not local), or when
`signal` aborts first.