Connect Handler
Last updated Oct 13, 2026
Background
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.
A Worker receives a connection in its connect() handler when another Worker forwards a TCP connection to it over a service binding, by calling connect() on the binding:
// Worker A forwards a TCP connection to Worker B over a service binding
export default {
async fetch(request, env) {
const socket = env.BACKEND.connect("example.com:1234");
// Read from and write to socket.readable / socket.writable
return new Response(socket.readable);
},
};
Note
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.
Syntax
export default {
async connect(socket, env, ctx) {
const writer = socket.writable.getWriter();
await writer.write(new TextEncoder().encode("hello"));
await writer.close();
},
};
interface Env {}
export default {
async connect(socket: Socket, env: Env, ctx: ExecutionContext) {
const writer = socket.writable.getWriter();
await writer.write(new TextEncoder().encode("hello"));
await writer.close();
},
} satisfies ExportedHandler<Env>;
You can also implement connect() as a method on a WorkerEntrypoint class, which lets a single Worker expose a connect() handler alongside other RPC methods:
import { WorkerEntrypoint } from "cloudflare:workers";
export class Backend extends WorkerEntrypoint {
async connect(socket) {
const writer = socket.writable.getWriter();
await writer.write(new TextEncoder().encode("hello"));
await writer.close();
}
}
Parameters
socketSocket- The inbound
Socketused to read and write raw bytes on the connection. socket.openedresolves with aSocketInfoobject. On aconnect()handler,localAddressis set to the address the caller passed toconnect()on the service binding.envobject- The bindings available to the Worker.
ctxobject- The context associated with the Worker, containing
waitUntil()andpassThroughOnException().
A Worker that does not export a connect() handler rejects any connection forwarded to it.
Related resources
- TCP sockets — Create outbound TCP connections and work with the
SocketAPI. - Service bindings — Call one Worker from another, including forwarding TCP connections with
connect().
Was this helpful?
YesNo
On this page
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/workers/runtime-apis/handlers/connect/#page","headline":"Connect Handler","description":"Handle inbound TCP connections forwarded to a Worker using the connect() handler.","url":"https://developers.cloudflare.com/workers/runtime-apis/handlers/connect/","inLanguage":"en","image":"https://developers.cloudflare.com/workers/runtime-apis/handlers/connect/og.png?v=abda5a093025e70d","dateModified":"2026-10-13","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
description: Process incoming emails with the email() handler in Cloudflare Workers to forward, reply, or reject messages. title: Workers API image: https://developers.cloudflare.com/email-service/api/route-emails/email-handler/og.png?v=bce7e41c11423abf
Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/email-service/llms.txt
Use this file to discover all available pages before exploring further.
Docs