demo · v131

TCPSocket: connect, write, read

Construct a TCPSocket with a host and port. The constructor returns a promise resolving with a writable and readable stream pair. This demo wires up an HTTP-like manual request — useful for talking to legacy protocols (SMTP, IRC, custom industrial endpoints) that don't speak HTTP.

flags + scheme Direct Sockets is exposed only inside an Isolated Web App (isolated-app:// scheme) with the direct-sockets permission policy. This page runs on a regular origin so the constructor will not exist — the probe below confirms.

connection log

response (read stream)

the api

const sock = new TCPSocket("example.com", 80);
const { readable, writable } = await sock.opened;

// Write
const writer = writable.getWriter();
await writer.write(new TextEncoder().encode("GET / HTTP/1.0\r\nHost: example.com\r\n\r\n"));

// Read
const reader = readable.getReader();
while (true) {
  const { value, done } = await reader.read();
  if (done) break;
  console.log(new TextDecoder().decode(value));
}

see also