On this page

    M

    dtls.connect

    History
    dtls.connect(host, port, options?): DTLSSession
    Attributes
    host:string
    Remote host to connect to, as an IPv4 or IPv6 literal. Host names are not resolved.
    port:number
    Remote port to connect to.
    options:Object
    ca:string | Buffer | string[] | Buffer[]
    CA certificates in PEM format.
    cert:string | Buffer
    Client certificate in PEM format.
    Client private key in PEM format.
    secureContext:DTLSSecureContext
    A 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:Buffer
    A session from session.session on an earlier connection, to resume rather than handshake in full. See Session resumption.
    passphrase:string
    Passphrase 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?:boolean
    When 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?:string
    Server 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?:string
    Local 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?:number
    Local bind port. Default: 0 (ephemeral).
    alpn:string[] | Buffer
    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:string
    SRTP protection profile names.
    mtu?:number
    Maximum size in bytes of a DTLS datagram. Default: 1200.
    handshakeTimeout?:number
    Milliseconds a handshake may take before it is abandoned and session.opened rejects. 0 disables it. Default: 60000. See Handshake timeout.
    Returns:DTLSSession

    Connects 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());
    };