On this page

    M

    dtls.connect

    History
    dtls.connect(host, port, options?): DTLSSession
    Attributes
    host:string
    Remote host to connect to.
    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.
    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 not verified. 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: '0.0.0.0'.
    bindPort?:number
    Local bind port. Default: 0 (ephemeral).
    alpn:string[] | Buffer
    ALPN protocol names.
    srtp:string
    SRTP protection profile names.
    mtu?:number
    Maximum transmission unit. Default: 1200.
    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('localhost', 4433, {
      ca: [readFileSync('ca-cert.pem')],
    });
    
    await session.opened;
    session.send('hello');
    
    session.onmessage = (data) => {
      console.log('Received:', data.toString());
    };