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 SocketOptions options = {});
            [SecureContext] Promise<UDPSocket> openUDPSocket(optional SocketOptions options = {});
          };
        

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

{{Navigator/openUDPSocket}} is used to open a 'client' UCP 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;

            unsigned long keepAlive;
            boolean noDelay;
          };
        

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.
keepAlive member
This is only relevant for TCP. It enabled TCP Keep-Alive and specifies the keep alive time, in seconds.
noDelay member
This is only relevant for TCP. It enables the TCP_NODELAY option, disabling Nagle's algorithm.
The only current implementation (Chromium) ignores {{SocketOptions/localAddress}} and {{SocketOptions/localPort}} and {{SocketOptions/keepAlive}}.

`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