Cloudflare Workers Introduces connect() API to Create TCP Sockets

Blog

HomeHome / Blog / Cloudflare Workers Introduces connect() API to Create TCP Sockets

Jul 06, 2023

Cloudflare Workers Introduces connect() API to Create TCP Sockets

InfoQ Homepage News Cloudflare Workers

InfoQ Homepage News Cloudflare Workers Introduces connect() API to Create TCP Sockets

May 28, 2023 2 min read

by

Renato Losio

During the recent developer week, Cloudflare announced a Worker API to create outbound TCP sockets. The new socket API allows developers to connect back to TCP-based infra directly from a Worker, including databases.

Available as a Runtime API, the connect() function returns a TCP socket that allows developers to read and write data until the connection remains open. Workers could already interact with HTTP endpoints and other Cloudflare services, but the vast majority of databases require clients to connect by opening a direct TCP socket. Brendan Irvine-Broque, product manager at Cloudflare, and Matt Silverlock, director of product at Cloudflare, explain:

With Workers, we aim to support standard APIs that are supported across browsers and non-browser environments wherever possible, (...) but for TCP sockets, we faced a challenge — there was no clear shared standard across runtimes. We’ve tried to incorporate the best elements of existing APIs and proposals, and intend to contribute back to future standards.

Last autumn Cloudflare, together with Vercel and Shopify, started WinterCG, a new community group, focused on the interoperable implementation of standardized web APIs in non-web browser, javaScript-based development environments.

The new API is accessed by importing the connect function from cloudflare:sockets. One of the common use cases is to create a connection to a database, for example:

Source: https://blog.cloudflare.com/workers-tcp-socket-api-connect-databases/

While pg, the JavaScript database driver for PostgreSQL, is already supported, the MySQL drivers mysql and mysql2 are not supported yet. Irvine-Broque and Matt Silverlock warn:

A new connection is created for every request. This is one of the biggest current challenges of connecting to databases from serverless functions, across all platforms (...) we’re already working on simpler approaches to connection pooling for the most popular databases.

The content delivery network expects to add more features in the future, including support for inbound TCP and UDP connections, as asked by some developers, as well as application protocols based on QUIC.

The connect() API was not the only new feature announced during the Developer Week 2023: Cloudflare introduced Secrets Store, a solution for managing application secrets securely, improvements to D1, Cloudflare's serverless database, and consumer concurrency for the messaging service Queues. Furthermore, Cloudflare announced database integrations for Neon, PlanetScale, and Supabase on Workers. Karl Horky, founder at UpLeveled, tweets:

No proxy like Neon or other serverless/edge providers, you just connect normally over TCP. This sounds great, potentially way bigger than the other recent edge database announcements.

Each open TCP socket counts towards the maximum number of open connections that can be simultaneously open in Workers and TCP connections cannot be created on port 25 to send email to SMTP mail servers.

Presented by: Sean Chittenden - Director of Engineering

Save your seat

Writing for InfoQ has opened many doors and increased career opportunities for me. I was able to deeply engage with experts and thought leaders to learn more about the topics I covered. And I can also disseminate my learnings to the wider tech community and understand how the technologies are used in the real world.

I discovered InfoQ's contributor program earlier this year and have enjoyed it since then! In addition to providing me with a platform to share learning with a global community of software developers, InfoQ's peer-to-peer review system has significantly improved my writing. If you’re searching for a place to share your software expertise, start contributing to InfoQ.

I started writing news for the InfoQ .NET queue as a way of keeping up to date with technology, but I got so much more out of it. I met knowledgeable people, got global visibility, and improved my writing skills.

Becoming an editor for InfoQ was one of the best decisions of my career. It has challenged me and helped me grow in so many ways. We'd love to have more people join our team.

InfoQ seeks a full-time Editor-in-Chief to join C4Media's international, always remote team. Join us to cover the most innovative technologies of our time, collaborate with the world's brightest software practitioners, and help more than 1.6 million dev teams adopt new technologies and practices that push the boundaries of what software and teams can deliver!

A round-up of last week's content on InfoQ sent out every Tuesday. Join a community of over 250,000 senior developers. View an example

We protect your privacy.

You need to Register an InfoQ account or Login or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Join a community of experts. Renato Losio has opened many doors and increased career opportunities Vivian Hu InfoQ's peer-to-peer review system has significantly improved my writing Oghenevwede Emeni got global visibility, and improved my writing skills Edin Kapić best decisions of my career helped me grow in so many ways join our team Thomas Betts full-time Editor-in-Chief The InfoQ Get the most out of the InfoQ experience.