function
quic.connect
Initiate a new client-side session.
import { connect } from 'node:quic';
import { Buffer } from 'node:buffer';
const enc = new TextEncoder();
const alpn = 'foo';
const client = await connect('123.123.123.123:8888', { alpn });
await client.createUnidirectionalStream({
body: enc.encode('hello world'),
});By default, every call to connect(...) will create a new local QuicEndpoint instance bound to a new random local IP port. To specify the exact local address to use, or to multiplex multiple QUIC sessions over a single local port, pass the endpoint option with either a QuicEndpoint or EndpointOptions as the argument.
import { QuicEndpoint, connect } from 'node:quic';
const endpoint = new QuicEndpoint({
address: '127.0.0.1:1234',
});
const client = await connect('123.123.123.123:8888', { endpoint });Referenced types
class SocketAddress
- @param input
An input string containing an IP address and optional port, e.g.
123.1.2.3:1234or[1::1]:1234.@returnsReturns a
SocketAddressif parsing was successful. Otherwise returnsundefined.
interface SessionOptions
- alpn?: string | readonly string[]
The ALPN (Application-Layer Protocol Negotiation) identifier(s).
For client sessions, this is a single string specifying the protocol the client wants to use (e.g.
'h3').For server sessions, this is an array of protocol names in preference order that the server supports (e.g.
['h3', 'h3-29']). During the TLS handshake, the server selects the first protocol from its list that the client also supports.The negotiated ALPN determines which Application implementation is used for the session.
'h3'and'h3-*'variants select the HTTP/3 application; all other values select the default application. - application?: ApplicationOptions
HTTP/3 application-specific options. These only apply when the negotiated ALPN selects the HTTP/3 application (
'h3'). - ca?: ArrayBuffer | ArrayBufferView<ArrayBufferLike> | readonly unknown[]
The CA certificates to use for client sessions. For server sessions, CA certificates are specified per-identity in the
sessionOptions.snimap. - cc?: 'reno' | 'cubic' | 'bbr'
Specifies the congestion control algorithm that will be used. Must be set to one of either
'reno','cubic', or'bbr'.This is an advanced option that users typically won't have need to specify.
- certs?: ArrayBuffer | ArrayBufferView<ArrayBufferLike> | readonly unknown[]
The TLS certificates to use for client sessions. For server sessions, certificates are specified per-identity in the
sessionOptions.snimap. - crl?: ArrayBuffer | ArrayBufferView<ArrayBufferLike> | readonly unknown[]
The CRL to use for client sessions. For server sessions, CRLs are specified per-identity in the
sessionOptions.snimap. - datagramDropPolicy?: 'drop-oldest' | 'drop-newest'
Controls which datagram to drop when the pending datagram queue (sized by
session.maxPendingDatagrams) is full. Must be one of'drop-oldest'(discard the oldest queued datagram to make room) or'drop-newest'(reject the incoming datagram). Dropped datagrams are reported as lost via theondatagramstatuscallback.This option is immutable after session creation.
- drainingPeriodMultiplier?: number
A multiplier applied to the Probe Timeout (PTO) to compute the draining period duration after receiving a
CONNECTION_CLOSEframe from the peer. RFC 9000 Section 10.2 requires the draining period to persist for at least three times the current PTO. The valid range is3to255. Values below3are clamped to3. - enableEarlyData?: boolean
When
true, enables TLS 0-RTT early data for this session. Early data allows the client to send application data before the TLS handshake completes, reducing latency on reconnection when a valid session ticket is available. Set tofalseto disable early data support. - handshakeTimeout?: number | bigint
Specifies the keep-alive timeout in milliseconds. When set to a non-zero value, PING frames will be sent automatically to keep the connection alive before the idle timeout fires. The value should be less than the effective idle timeout (
maxIdleTimeouttransport parameter) to be useful. - keylog?: boolean
When
true, enables TLS key logging for the session. Key material is delivered to thesession.onkeylogcallback in NSS Key Log Format. Each callback invocation receives a single line of key material. The output can be used with tools such as Wireshark to decrypt captured QUIC traffic. - maxDatagramSendAttempts?: number
The maximum number of
SendPendingDatacycles a datagram can survive without being sent before it is abandoned. When a datagram cannot be sent due to congestion control or packet size constraints, it remains in the queue and the attempt counter increments. Once the limit is reached, the datagram is dropped and reported as'abandoned'via theondatagramstatuscallback. Valid range:1to255. - minVersion?: number
The minimum QUIC version number to allow. This is an advanced option that users typically won't have need to specify.
- preferredAddressPolicy?: 'ignore' | 'default' | 'use'
When the remote peer advertises a preferred address, this option specifies whether to use it or ignore it.
- reuseEndpoint?: boolean
When
true(the default),connect()will attempt to reuse an existing endpoint rather than creating a new one for each session. This provides connection pooling behavior — multiple sessions can share a single UDP socket. The reuse logic will not return an endpoint that is listening on the same address as the connect target (to prevent CID routing conflicts).Set to
falseto force creation of a new endpoint for the session. This is useful when endpoint isolation is required (e.g., testing stateless reset behavior where source port identity matters). - sessionTicket?: ArrayBufferView<ArrayBufferLike>
A session ticket to use for 0RTT session resumption.
- sni?: Record<string, SNIEntry>
An object mapping host names to TLS identity options for Server Name Indication (SNI) support. This is required for server sessions and must contain at least one entry. The special key
'*'specifies the optional default/fallback identity used when no other host name matches. If no wildcard entry is provided, connections with unrecognized server names will be rejected with a TLSunrecognized_namealert. Each entry may contain: - token?: ArrayBufferView<ArrayBufferLike>
An opaque address validation token previously received from the server via the
session.onnewtokencallback. Providing a valid token on reconnection allows the client to skip the server's address validation, reducing handshake latency. - unacknowledgedPacketThreshold?: number | bigint
Specifies the maximum number of unacknowledged packets a session should allow.
- verifyPrivateKey?: boolean
True to require private key verification for client sessions. For server sessions, this option is specified per-identity in the
sessionOptions.snimap. - version?: number
The QUIC version number to use. This is an advanced option that users typically won't have need to specify.
namespace QuicSession
class Stats