Skip to content
AnswerWell geo Documentation

Research · 6 min read · Answerwell

Workers TCP Sockets, connect Handler, Request Signal, and Node net

How outbound TCP connect(), inbound connect handlers, client cancellation, waitUntil, env, Request.cache, and node:net/dns/tls work on the Workers runtime.

This page summarises Cloudflare Workers documentation on raw TCP networking, the connect() runtime API, the connect() handler for connections forwarded over service bindings, and related runtime changes: Request.signal, global-scope AbortController, imported waitUntil and env, subrequest Request.cache, and Node.js-compatible node:net, node:dns, and node:tls.

TCP sockets: outbound connect() from a Worker

Section titled “TCP sockets: outbound connect() from a Worker”

Outbound TCP on Workers uses the connect() function from cloudflare:sockets, not a literal IP in fetch().

The Workers runtime provides the connect() API for creating outbound TCP connections ↗︎ from Workers.

— Cloudflare, TCP sockets

connect() returns a Socket with readable and writable streams so you can speak database wire protocols, MQTT, SMTP (with limits), and similar TCP-based systems. For PostgreSQL, the docs steer you toward Hyperdrive rather than hand-rolling sockets.

connect() accepts a SocketAddress or a string, optional SocketOptions, and returns a Socket. Many servers expect opportunistic TLS: create the socket with secureTransport: "starttls", then call startTls() once to upgrade; the original socket must not be used after that.

For resources inside a VPC Network binding, connect() on the binding opens plaintext TCP to private addresses:

Open raw TCP connections to any private destination using connect(). This is useful for non-HTTP protocols like Redis, Memcached, MQTT, or custom binary protocols

— Cloudflare, VPC Networks

See also Workers Binding API — connect().

connect() handler: accept TCP forwarded from another Worker

Section titled “connect() handler: accept TCP forwarded from another Worker”

A Worker that exports connect() accepts an inbound TCP connection when another Worker forwards it via a service binding, not from the public Internet.

Export a connect() handler to let a Worker accept an inbound TCP connection and read and write raw bytes on it. The handler receives a Socket object, the same type returned by connect() from cloudflare:sockets for outbound connections.

— Cloudflare, Connect Handler

Direct inbound TCP connections from the public Internet, for example by using the HTTP CONNECT method against a Worker’s public URL, are not supported at present. A connect() handler only receives connections forwarded from another Worker over a service binding.

— Cloudflare, Connect Handler

On the backend, socket.opened resolves with localAddress set to the address the caller passed to connect() on the binding. Production no longer requires an experimental flag for this handler (connect handler changelog). A Worker without connect() rejects forwarded connections (Connect Handler — Parameters).

Direction Mechanism Public Internet inbound
Outbound TCP import { connect } from 'cloudflare:sockets' N/A (outbound only)
Inbound TCP (today) connect() handler + service binding Not supported
HTTP / HTTPS fetch() handler / fetch() subrequests Supported

Protocols reference lists HTTP, WebSockets, forwarded TCP, and related interfaces in one table.

Node.js node:net, node:dns, and node:tls on Workers

Section titled “Node.js node:net, node:dns, and node:tls on Workers”

With Node.js compatibility enabled (or default on newer compatibility dates per node:net runtime doc), Workers map node:net socket operations onto the same underlying cloudflare:sockets behaviour.

node:net supports net.Socket, net.BlockList, and net.SocketAddress; net.Server is not supported (Support for Node.js DNS, Net, and Timer APIs, node:net).

node:dns resolves names via DNS over HTTPS to 1.1.1.1. All node:dns functions are available except lookup, lookupService, and resolve, which throw “Not implemented” (same changelog).

node:tls adds connect, TLSSocket, checkServerIdentity, and createSecureContext for TLS to external services (Improved support for Node.js Crypto and TLS APIs).

Use node:* when porting Node libraries; prefer cloudflare:sockets when you want the documented Worker-native API and StartTLS helpers (TCP sockets).

Request.signal and AbortController: client disconnect and global scope

Section titled “Request.signal and AbortController: client disconnect and global scope”

Listen for client cancellation with Request.signal

Section titled “Listen for client cancellation with Request.signal”

When a client aborts a request to your Worker, you can run cleanup or logging by listening on request.signal. This requires the enable_request_signal compatibility flag.

In Cloudflare Workers, you can now attach an event listener to Request objects, using the signal property ↗︎. This allows you to perform tasks when the request to your Worker is canceled by the client.

— Cloudflare, Handle incoming request cancellation in Workers with Request.signal

Pairing a long-lived response body with scheduler.wait() in a loop is a documented pattern to keep the connection open until abort fires (same post). scheduler.wait() itself accepts an optional signal and rejects with AbortError when aborted (Scheduler).

new AbortController(), AbortSignal.abort(), new WritableStream(), and request.signal no longer throw during module evaluation (top-level Worker code). That supports shared cancellation across requests—for example in Durable Objects—but AbortSignal.timeout() still needs an active request context because it schedules a timer (AbortController and AbortSignal can be created at global scope). Web standards — AbortController and AbortSignal describes the standard cancellation model.

waitUntil and env without passing ctx through every function

Section titled “waitUntil and env without passing ctx through every function”

Import waitUntil from cloudflare:workers to extend execution after the response is sent—the same semantics as ctx.waitUntil(), without threading ctx through helpers (Directly import waitUntil).

Import env from cloudflare:workers to read bindings (secrets, vars, stubs) anywhere in the codebase. Top-level env still cannot perform arbitrary I/O: vars and secrets work; you may obtain a Durable Object stub at top level, but calling KV, other Workers, or methods on that stub outside a request context does not (Import env).

Subrequest caching: Request.cache and cf.vary

Section titled “Subrequest caching: Request.cache and cf.vary”

Setting cache: "no-store" on a Request used in a Worker subrequest bypasses Cloudflare cache lookup and storage for that subrequest, even when Cache-Control would allow caching—fixing prior runtime errors and improving npm client compatibility. Use compatibility date on or after 2024-11-11 or enable cache_option_enabled (Bypass caching with Request.cache).

fetch() subrequests can pass cf.vary to normalise or bypass cache keys for origins that send Vary (Workers fetch requests now support cf.vary).

Common mistakes when using TCP and signals on Workers

Section titled “Common mistakes when using TCP and signals on Workers”

connect() on WorkerEntrypoint is reserved for inbound forwarded sockets (Reserved Methods — connect()).

  1. Confirm your Worker’s compatibility date and flags match the feature you use (enable_request_signal, cache_option_enabled, or defaults described in node:net).
  2. For outbound TCP, implement a minimal fetch() handler using TCP sockets — connect() and verify you connect with a hostname (not a blocked IP range or port 25).
  3. For inbound TCP, deploy a frontend Worker that calls env.BACKEND.connect(...) and a backend with a Connect Handler; confirm socket.opened reports the expected localAddress.
  4. Abort a long-running client request and confirm your request.signal listener runs (Handle incoming request cancellation).
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page disagrees with your Worker behaviour or the linked Cloudflare docs, email yimasynthetic@gmail.com with the page URL and the exact sentence you are questioning.