Cloudflare Workers integration is now possible

Let’s hope for the best then.
Any workaround for this issue in the meantime?

Until Cloudflare adds native support for TLS to their workers the workaround is to leverage MongoDB’s Data API instead (see Create a REST API with Cloudflare Workers and MongoDB Atlas | MongoDB for an example of this)

1 Like

You can also do this using a service binding with an RPC worker. One would still use Realm, but instead of making calls to the database via REST/http requests one can call methods directly from main app. It is explained in this blog post from Cloudflare.
We've added JavaScript-native RPC to Cloudflare Workers.
I build a test app based on todo-list example in the REST-API, I could share the code if anyone is interested.

Hi MongoDB @BlindChicken, Please do share the code to connect to mongodb using cf workers.

I have been using the data api, and while it works with workers, the limitations such as the inability to perform simple queries such as findOneAndUpdate and the 2000 ms connection time is greatly limiting our decision to use Mongodb in future projects.

Even using serverless database option provided by cf, i belive the nodes are not distributed globally is that right?

Really looking to speed up Data API access or finding a driver that would work with cf workers. – your update would be appreciated.

Now that MongoDB has suddenly deprecated Realm Sync and the DataAPI this leaves us in the dust. What are the alternatives? We may be switching over to cloudflare D1 or using hyperdrive with postgres to solve our solution. MongoDB please update the driver to support cloudflare workers.

This workaround is no longer viable with the Data API deprecation. Hopefully someone at Mongo can put a little pressure on Cloudflare to add TLS worker support.

Some good news Cloudflare seems to have solved it for us, I am yet to test it, but looks like the native mongodb driver now works with workers.

@Rishi_uttam TL;DR - you still can’t use MongoDB’s Node.js driver from Cloudflare Workers even after their latest round of updates.

I’ve written this up in a bit more detail at Why Cloudflare Workers Don't Work With MongoDB | ALEX BEVILACQUA, but it seems like we’re still not quite there.

Excellant write up, and you also addressed previous MongoDB blog posts that are now invalidted due to the deprecation.

If would be amazing if you can report this to the issues channel on github as advised in the blog post here: Sign in to GitHub · GitHub

Hopefully someone would look at it and atleast remove mongodb from the post so as to not confuse the rest of us.

I am now going to look in to neurelo but another monthly fee just to get this all working seems very hacky right now. Last option is to try realm-web which I have used in production before, however its as slow as the old data api, but does allow me to bypass server side and write the connection script on the front end (with the added load of 200kb for real-web js) Wait has realm-web been deprecated too?! nothing is very clear at this point.

Thanks

There are some community solutions cropping up for the Data API (see discussion at Data API and HTTPS Endpoints are Deprecated? - #14 by Sven_Glockner).

I haven’t validated any of these personally, but there would definitely be options to explore that don’t require a subscription.

If would be amazing if you can report this to the issues channel on github as advised in the blog post here: Sign in to GitHub · GitHub

I’ve opened 🐛 BUG: `mongodb` can be imported but not successfully used with nodejs_compat_v2 · Issue #6684 · cloudflare/workers-sdk · GitHub to continue tracking this issue.

@alexbevi any updates on this? there’s no movement on github nor anywhere here… I heard from others they just moved to Azure or use a proxy, first one would be what I’m going to do next if there’s no movement till maybe jan/feb and latter is really not the way to go, for me.

can’t you just force them? it’s pretty ridiculous that it’s been YEARS… or at least give a good explanation WHY it takes so long or why they don’t want to implement it…

don’t think its possible to force developers, plus it does seem like a infrastructure issue as cloud flare workers isn’t a runtime like Node, its a worker thread, similar to the one in the browser.

Hey @A_B7,

We can’t force Cloudflare to do anything, but based on mongodb · cloudflare/workerd · Discussion #2721 · GitHub it sounds like there might be some movement on this in January.

More information will be shared as it becomes available, but I’d recommend following that discussion thread as well for the time being.

HELLO EVERYBODY! I got it to work!

I was able to get the mongodb driver to work in cloudflare workers using module-aliases and TCP sockets!

Its a very minimal implementation and I would really be very grateful for any contributions to the repository :grinning:

I was able to achieve this by using module-aliases to polyfill the net.createConnection and tls.connect functions and make it return a mock Socket that utilizes cloudflares TCP sockets

The repository can be found here

2 Likes

James you are the BEST! I just tried it out and it appears to work. I only tried on a single call to the database but it worked. I’m using it within a React-Router7 app deployed on workers.

There remains one small issue, not with the polyfill, but with the workers/mongodb Atlas integration. The problem is that network access to your cluster is only possible via IPv4, and CF-workers all use the the same IPv6 address, specifically 2a06:98c0:3600::103. As I see it, one needs to allow open access to the cluster, which really isn’t great for security.

1 Like

npm error 404 Not Found - GET https://registry.npmjs.org/cf-mongodb-polyfills - Not found

Going to download it right now and manually try it :wink:

…or can you… :smiley:

Not sure why, but the link in James’ post is now dead. But here are the links:
Repo: https://github.com/jchoi2x/cf-mongodb-polyfills
NPM(github): Package cf-tcp-mock · GitHub
And there seems to be an issue Github, pages are really slow to load.

it seems to be working. but getting some weird cross promise resolution issues connecting and disconnecting db in the index.ts
any suggestion on how to handle connection and disconnection?

what’s my reward? :wink:

2 Likes