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(): hostname, port, and StartTLS
Section titled “connect(): hostname, port, and StartTLS”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.
Private networks with VPC connect()
Section titled “Private networks with VPC connect()”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).
AbortController at module top level
Section titled “AbortController at module top level”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”- Opening TCP at module scope or reusing one socket across requests (TCP sockets — Considerations).
- Expecting public inbound TCP on a Worker URL; only service-binding forwarding works today (Connect Handler).
- Calling
node:dnslookup/resolveinstead ofdns.promises.resolve4and related APIs (nodejs compat improvements). - Using
fetch('http://192.0.2.1')instead of a zone hostname (known issues). - Forgetting
enable_request_signalbefore relying onrequest.signal(request cancellation changelog). - Assuming top-level
envcan call KV or Durable Object methods (Import env).
connect() on WorkerEntrypoint is reserved for inbound forwarded sockets (Reserved Methods — connect()).
How to check this on your site
Section titled “How to check this on your site”- 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). - 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). - For inbound TCP, deploy a frontend Worker that calls
env.BACKEND.connect(...)and a backend with a Connect Handler; confirmsocket.openedreports the expectedlocalAddress. - Abort a long-running client request and confirm your
request.signallistener runs (Handle incoming request cancellation). - You can check automatically in Answerwell (our tool): https://answerwell.org/workspace
Report an error or ask a question
Section titled “Report an error or ask a question”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.