Cloudflare Workers integration is now possible

Today, Cloudflare announced their support for TCP sockets in Workers, along with day-1 support for Postgres.

I would love to see some form of first class support, either in the main package or through another package, but I can’t think of a good way to do this (aside from forking the Node driver). I took a look at Postgres’ solution here; they check if net.Socket exists, otherwise the Cloudflare implementation is used instead. Seems straightforward, there might be a more elegant way to make that check.

What are other peoples’ thoughts on this? Would something like this be worth a pull request?

1 Like

Hey @Cole_Crouter, I’m the Product Manager for the Node.js Driver so I’d be happy to chime in here.

Our engineers provided early feedback on the Socket API design, however we have not yet completed a review of the final product Cloudflare released this week.

This is something we are reviewing in detail and plan to share more information on as we progress. We always welcome feedback and participation from the developer community, so if you have ideas either open a ticket in our Jira under the NODE project or continue to ask questions here on our forums.

Hi @alexbevi - that sounds very interresting :nerd_face:

Any updates on this project? Would be amazing to be able to connect to MongoDB from a Cloudflare worker (without having to use a slow HTTP API or data proxy).

1 Like

Hi @Alex_Bjorlig, and apologies for the delay in getting back to you.

Cloudflare Workers aren’t 100% API compatible with Node.js, though they do offer a nodejs_compat compatibility flag to improve this developer experience.

The MongoDB Node.js Driver leverages a number of Node.js APIs internally, which our team found during preliminary testing weren’t compatible with Cloudflare Workers (even using nodejs_compat).

We are working on improving this as it currently prevents users from using the MongoDB Node.js Driver in this environment at all. Once we have this functional and understand the implications for our developers we’ll have more information to share.

1 Like

No worries :pray:

We are deployed with Vercel, and are slowly and steadily preparing our application for the edge. Because we use MongoDB, this is a critical step of the journey.

We expect the edge to make the app faster, as we won’t have cold starts (and getting closer to the end-user for non-data interactions)

You can always reach out if you need someone to test initial iterations or similar. (And if we can help in other ways, please let us know :nerd_face:)

1 Like

Também estamos esperando isso para trazer todos nossos bancos de dados para a MongoDB.

Hi Alex,

Wanted to check in to see if there was any progress of using the officials mongo driver now on cw workers? Is this something that the team is working towards (to have more support for node packages) or will workers always have this limitation…

We’re using the dataAPI which is slow, but works, moving forward would like a more permanent scalable solution.

Disclaimer; I’m not affiliated with MongoDB - just a “normal” customer.

I hope/think MongoDB is really busy at the moment, making the official MongoDB driver compatible with the worker’s environment. Cloudflare recently shared this post, and the feature set is impressive. Combined with Cloudflare’s work on Hyperdrive - it seems like a lot of effort is being put into how to make initial connections fast. From the article:

…it maintains a set of regional database connection pools across Cloudflare’s network, so a Cloudflare Worker avoids making a fresh connection to a database on every request. Instead, the Worker can establish a connection to Hyperdrive (fast!), with Hyperdrive maintaining a pool of ready-to-go connections back to the database. Since a database can be anywhere from 30ms to (often) 300ms away over a single round-trip (let alone the seven or more you need for a new connection), having a pool of available connections dramatically reduces the latency issue that short-lived connections would otherwise suffer.

I hope MongoDB is actively working on:

  1. Make the native node.js driver work on workers
  2. In cooperation with Cloudflare offer “regional database connection pools”, so a Cloudflare Worker avoids making a fresh connection on every request.

Hopefully, we soon get more information - so MongoDB can be an attractive alternative to Hyperdrive/D1. Would be such a shame if MongoDB misses the open window they have right now to be an early adopter of the edge.

1 Like

Excellent post Alex. I do hope the Atlas team takes heed of this advice, many startups are looking towards serverless data base at the edge with a quick response time, and if Mongo fails to adapt with the tools that are already out there then consumers may depart to greener pastures.

Any updates here? Would be incredible to see MongoDB moving fast in this space :pray:

Hi @Alex_Bjorlig,

We are working with Cloudflare on this still, however at the moment we are blocked as Atlas requires TLS for all connections, however Cloudflare Workers only supports mTLS at the moment (and TLS cannot be disabled in Altas).

1 Like

Thanks for providing an update :+1:
And is it completely unrealistic that Atlas could support mTLS somehow? Or is there another way to workaround this issue?

@Alex_Bjorlig the challenge for the moment is that with only mTLS available we cannot provide our own TLS certificate/key to the socket layer. This prevents us from being able to support connections to MongoDB Atlas clusters.

We’ll continue to work with Cloudflare on proper TLS support though, and share updates as they become available.

Hi @alexbevi - I hope you had a great New Year :confetti_ball:

Do you think it’s realistic that some progress will happen on this TLS topic, or is it better that we start looking for a different solution?

Unfortunately I’m waiting for further info regarding Cloudflare’s roadmap for their Workers product. Once I know more we can schedule work on our end accordingly and share this out more broadly, but for now there’s been no further progress.

1 Like

Hoping Cloudflare gets back to you soon :crossed_fingers: I’m also really looking forward to using Mongodb with Cloudflare workers.

1 Like

@alexbevi just to check, does the Mongo Driver require some special variant of TLS? You should be able to enable regular TLS via the socket API like so:

const socket = connect("atlas.mongodb.com:27017", {
	secureTransport: "on" 
});

I know that URL/Port combo probably isn’t right, but you get the gist.
Documentation

Thanks for the feedback @HelloImAlastair, but the challenge we’re facing is that Cloudflare only supports Client authentication with mTLS - not TLS. Without the ability for Cloudflare Workers to provide client CA access within the Worker runtime we cannot provide secure connections to MongoDB Atlas from this environment.

1 Like