This specification defines an API that allows web applications to talk to servers and devices that have their own protocols incompatible with those available on the web.

API definition

Extensions to the `Navigator` interface

          partial interface Navigator {
            [SecureContext] Promise<TCPSocket> openTCPSocket(optional TCPSocketOptions options = {});
            [SecureContext] Promise<UDPSocket> openUDPSocket(optional UDPSocketOptions options = {});
          };
        

{{Navigator/openTCPSocket}} is used to open a client TCP socket.

{{Navigator/openUDPSocket}} is used to open a 'client' UDP socket, that only exchanges datagrams with a specific remote address and port.

The API currently provides no support for server sockets, that can receive packets from arbitrary remote addresses and ports.

User agents that do not support direct sockets SHOULD NOT expose {{Navigator/openTCPSocket()}} or {{Navigator/openUDPSocket()}} on the {{Navigator}} interface.

`SocketOptions` dictionary

          dictionary SocketOptions {
            DOMString localAddress;
            unsigned short localPort;

            DOMString remoteAddress;
            unsigned short remotePort;

            unsigned long sendBufferSize;
            unsigned long receiveBufferSize;
          };
          

The SocketOptions dictionary consists of several optional members:

localAddress member
The IP address (or hostname) on the local host that the socket should be bound to.
localPort member
The port that the socket should be bound to. When omitted, or 0, any port can be used.
remoteAddress member
The IP address (or hostname) of the host that the socket will connect to.
remotePort member
The port on the remote host, that the socket will connect to.
sendBufferSize member
The requested send buffer size, in bytes.
receiveBufferSize member
The requested receive buffer size, in bytes.
The only current implementation (Chromium) ignores {{SocketOptions/localAddress}} and {{SocketOptions/localPort}}.

`TCPSocketOptions` dictionary

          dictionary TCPSocketOptions : SocketOptions {
            boolean noDelay;
            boolean keepAlive;
            [EnforceRange] unsigned long keepAliveDelay;
          };
          

The {{TCPSocketOptions}} dictionary inherits all attributes from {{SocketOptions}} and exposes optional TCP-specific options.

noDelay member
Enables the `TCP_NODELAY` option, disabling Nagle's algorithm.
keepAlive member
Enables/disables TCP Keep-Alive by setting `SO_KEEPALIVE` option on the socket.
keepAliveDelay member
Specifies the Keep-Alive ping delay in milliseconds. Must be specified only if {{TCPSocketOptions/keepAlive}} is set to `true`.

`UDPSocketOptions` dictionary

          dictionary UDPSocketOptions : SocketOptions {};
          

The UDPSocketOptions dictionary inherits all attributes from {{SocketOptions}}.

`TCPSocket` interface

          [Exposed=Window, SecureContext] interface TCPSocket {
            Promise<undefined> close();

            readonly attribute DOMString remoteAddress;
            readonly attribute unsigned short remotePort;
            readonly attribute ReadableStream readable;
            readonly attribute WritableStream writable;
          };
        
The only current implementation (Chromium) does not support "byob" mode for {{TCPSocket/readable}}. See [[streams]] for more information about streams.

`UDPSocket` interface

          [Exposed=Window, SecureContext] interface UDPSocket {
            async iterable<SocketOptions, Blob>;

            Promise<undefined> send(Blob data);
            Promise<undefined> close();

            readonly attribute DOMString remoteAddress;
            readonly attribute unsigned short remotePort;
          };
          
{{SocketOptions/remoteAddress}} and {{SocketOptions/remotePort}} are the only members set in the {{SocketOptions}} returned by the iterator.

Usage Example

Clients can asynchronously iterate to read incoming datagrams. See [[fileapi]] for more information about blobs.

            for await (let [source, blob] of udpSocket) {
              console.log('Received ' + blob.size + ' bytes from ' + source.remoteAddress);
            }
          

Accessibility

This specification has no known accessibility considerations.

Security and privacy considerations

Permissions Policy integration

This specification defines a policy-controlled permission identified by the string "direct-sockets". Its default allowlist is '`self`'.

A document’s permission policy determines whether a {{Navigator/openTCPSocket(...)}} or {{Navigator/openUDPSocket(...)}} call immediately rejects with a {{"NotAllowedError"}} {{DOMException}}.