dtls.connect(host, port, options?): DTLSSession
Attributes
host:
stringRemote host to connect to, as an IPv4 or IPv6 literal.
Host names are not resolved.
port:
numberRemote port to connect to.
options:
ObjectsecureContext:
DTLSSecureContextA context from
dtls.createSecureContext() to use instead of building one from the
credential options below. Must not have been created with
isServer: true. Cannot be combined with any option the context already
carries.A pre-shared key as
{ identity, key }, or a
function returning one. See Pre-shared keys.session:
BufferA session from
session.session on an earlier
connection, to resume rather than handshake in full. See
Session resumption.passphrase:
stringPassphrase to decrypt
key, if it is encrypted.
Ignored when key is not encrypted. Unlike key and cert, this must be
a string, matching tls.createSecureContext().rejectUnauthorized?:
booleanWhen
true, the server's certificate must
both chain to a trusted CA and match the expected identity (servername,
or host when servername is not set); otherwise the handshake is
aborted and session.opened rejects. When false, the certificate is
still verified and the handshake completes regardless, leaving the
decision to the application via session.authorized and
session.authorizationError. Default: true.servername?:
stringServer name used for the SNI (Server Name
Indication) extension and as the identity checked during certificate
verification. Default: the
host argument. Set to '' to disable SNI.
SNI is never sent for IP address literals.bindHost?:
stringLocal bind address. Default:
'::' when host is an
IPv6 literal, otherwise '0.0.0.0'. The local socket must be in the same
address family as the peer.bindPort?:
numberLocal bind port. Default:
0 (ephemeral).ALPN protocol names. Each name must be between
1 and 255 bytes. A
Buffer must already be in ALPN wire format: one
length byte followed by that many bytes, repeated.srtp:
stringSRTP protection profile names.
mtu?:
numberMaximum size in bytes of a DTLS datagram. Default:
1200.handshakeTimeout?:
numberMilliseconds a handshake may take before it is
abandoned and
session.opened rejects. 0 disables it. Default:
60000. See Handshake timeout.Returns:
DTLSSessionConnects to a DTLS server. Returns a DTLSSession whose opened property
is a Promise that resolves when the handshake completes.
import { connect } from 'node:dtls'; import { readFileSync } from 'node:fs'; const session = connect('127.0.0.1', 4433, { ca: [readFileSync('ca-cert.pem')], }); await session.opened; session.send('hello'); session.onmessage = (data) => { console.log('Received:', data.toString()); };