read 7 min
51 / 51
Apr 5

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.

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

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.

10 days later

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?

17 days later

Awesome, is this/can this be an official release that workers now support Mongodb drivers? The community solve the data api deprecation!

It is very exciting that Cloudflare is making progress on this. I haven’t had a chance to test these changes yet, but given this is just adding support for node:net, to connect to Atlas they’ll likely still need to implement node:tls.

Hello Ben,
I am also getting the same error BSONOffsetError but I am only getting it intermittently. I am certain that it is not related to the polyfill. As far as I can tell, it is likely due to a memory limit, or some other limit. I’m getting the error on M0 free cluster, when doing $vectorSearch. I can prevent it from occurring by setting a low limit, eg: 1 or 2 docs, as opposed to 10 or more. I don’t see any errors in my MongoAtlas dashboard, nor do any appear in Cloudflare.

If I run my app in locally in dev mode (not using wrangler) I don’t get the error, but with Wrangler, in preview or deployed, I do get the errors. So not really sure what is causing the issue, but it is frustrating.

I should probably file an issue, but I’m not sure where, any suggestion?

2 months later

Thank for the test. Unfortunately i’m still getting a
“MongoServerSelectionError: socket.once is not a function”

because @alexbevi is, according to his own blog post, fully responsible for this fix release, you should hold him accountable for any issues :wink:

pretty sure that’s not what I’m implying (though I might have made the most noise) … but based on the error it does’t seem the workerd was updated to pull in the latest dependencies.

Maybe, though i followed your nice blog post

wrangler.toml

compatibility_flags = [“nodejs_compat_v2”]
compatibility_date = “2025-03-20”
pages_build_output_dir = “.svelte-kit/cloudflare”

I will have a look again i might have missed something (for now i’m staying on Vercel)