On this page

    M

    dtls.listen

    History
    dtls.listen(callback, options): DTLSEndpoint
    Attributes
    callback:Function
    Called for each new DTLS session accepted by the server.
    session:DTLSSession
    The new session.
    options:Object
    cert:string | Buffer
    Server certificate in PEM format. Required.
    Server private key in PEM format. Required.
    port:number
    Port to bind to. Required.
    host?:string
    Address to bind to. Default: '0.0.0.0'.
    ca:string | Buffer | string[] | Buffer[]
    CA certificates in PEM format.
    ciphers:string
    OpenSSL cipher list string.
    alpn:string[] | Buffer
    ALPN protocol names.
    srtp:string
    Colon-separated SRTP protection profile names (e.g., 'SRTP_AES128_CM_SHA1_80:SRTP_AEAD_AES_128_GCM').
    requestCert?:boolean
    Request client certificate. Default: false.
    mtu?:number
    Maximum transmission unit for DTLS records. Default: 1200.
    Returns:DTLSEndpoint

    Creates a DTLS server bound to the specified address and port. The server uses automatic HMAC-based cookie exchange for DoS protection.

    import { listen } from 'node:dtls';
    import { readFileSync } from 'node:fs';
    
    const endpoint = listen((session) => {
      session.onmessage = (data) => {
        console.log('Received:', data.toString());
        session.send('pong');
      };
    
      session.onhandshake = (protocol) => {
        console.log('Handshake complete:', protocol);
      };
    }, {
      cert: readFileSync('server-cert.pem'),
      key: readFileSync('server-key.pem'),
      port: 4433,
    });
    
    console.log('DTLS server listening on', endpoint.address);