Could I connect my front-end directly to Atlas?

Hi, the community,

I’m developing a small web app that can store my movies watch list. I’m storing the data to the browser’s local storage and going to make it online so that I can access it from anywhere.

Because the application is really small, I don’t want to set up a back-end between the web app and database cloud. So as the title, could I connect my front-end directly to the Atlas without using any back-end? Or could I just call APIs to CRUD data in Atlas directly from my clients?

Thank you,

1 Like

Hi @IM_Coder,

Welcome to MongoDB community.

For this use case a Realm application with a realm-web sdk is the perfect solution.

https://docs.mongodb.com/realm/get-started/introduction-web

This is the most easy and optimized way to focus on your front-end app while having an elastic managed backend. Realm apps have a generous free tier therefore you should be good.

Thanks
Pavel

1 Like

MongoDB just launched the Atlas Data API, which allows you to perform CRUD operations on your Atlas data through simple HTTP requests https://www.mongodb.com/docs/atlas/api/data-api/

1 Like

Using the data api is it secure enough to put the end points on the front end? for writing to database?

putting end-points to the front end? unless it is “read-only”, that would mean anyone would have access to your database and result in havoc.

For read-only purposes, this direct connection is great. you would just be working on a functional/dynamic web content such a stock-market following.

But for write access, it is a whole lot of story. Security is the main concept here and you would not want free access to your database. The usual way is to have your own back-end API to communicate with your database and so keep your database credentials secure (as much possible as your host settings allows).

Using Realm or Data API is best to use with your IoT devices as they mostly don’t have enough memory to put whole drivers. They can communicate with basic TCP requests to write to the database. As long as you keep those devices in safe places, you can have as many as you want to write to the database. or at least give them access to a very limited resource.

Or you may do the same base access from the terminal anytime with tools like good old curl. Or write PoC API fast without going into driver details (Javascript or Python is great for prototyping). Same applies to front-end; for PoC purposes create temporary access points.

Thank you for that detailed reply. That all makes sense and was how I thought the data api would work on the front end. My goal in finding a new way to connect to my database was speed more than anything else.

The other option is to use the realm-web sdk and this would solve my problem of calling the db from the front end. However its a big js bundle as I learned in one of my previous projects. Downloading realm 132kb unminified just to establish a connection and send data/authenticate is a bit much.

When i tried the data api, this is what I found:
I used Atlas 3rd party http triggers as a proxy to call the DATA API, but under my tests in the past it takes about 3-4 seconds to return a response .

  • Client (browser) calls mongo function endpoint (one of only 4 regions, none of which are where my db is stored) about 800ms
  • Mongo function calls the Data API which doesn’t hit the database directly but a proxy which is hosted in a US region 1500 ms
  • Finally the region where my database is hosted (Hong Kong on Atlas AWS) is hit and returns a response 200 ms.
    Seems like i went all the way around the world to get a response from within the same region where it was requested.

The whole cycle using the data api takes about 2.5 seconds on the quick side.

Realm SDK is much faster in my tests but the bundle is quite big.

So I am going back to what I did in the past which is simply use AWS lambda HK region running the official mongo db driver, which connects directly to my database. When the function is warm i get results in under 50 ms. – I wish this was the same with the data api, but its up to 10x as long. I hope Mongo Atlas can figure this out. Even calling a cloudflare edge worker still results in the same delay when calling the data api (as its still in preview, the data api end points are only in a few regions)

Sorry i went on a tangent, but duration and bundle size is our current problem, surely writing less backend code would come in super handy. Currently we using realm-web sdk for larger projects with good internet connections, but for smaller projects with mobile connections realm sdk is too much weight.

Anyway thanks for your help,.

1 Like

Hi Pavel, Yimaz,

Now that the DATA API is deprecated, I am unsure if the realm-web sdk which was discussed here as the perfect solution is deprecated to/or unmarinated?

Thanks

it is unfortunate…

it is very sad… is their another solution? mongodb official points us to use third party services like Hasura at a fee higher then mongodb atlas it self… the situation for many is dire; and there does not seem to be any official answer/solution… the propert thing to do would have been to deprecate one solution but offer a better native alternate (without outsourcing the problem)

… i am worried for the trajectory of new developers on mongodb going forward.

given that this is the community board, what is the advice from supporters?

Hi @Rishi_uttam, the deprecation docs page details various alternative solutions including multiple open source frameworks that can be used in combination with mongodb drivers, or cloud native functions with drivers. We have seen customers have success with both of those options if the partner solutions are not a fit for your use case. Here is the docs link with more information - https://www.mongodb.com/docs/atlas/app-services/data-api/data-api-deprecation/#data-api-and-https-endpoints-deprecation

1 Like

Thanks Laura; appreciate your reply.

I could not find a solution for my use case. A number of us are using the Data API with Cloudfalre workers. We have 40-50 endpoints set up using the Data API via cloudflare workers as our backend. Since CF runs on V8 engine the Data API was a great choice. Now the alternative mentioned in the link you provided asks use to migrate to Azure/Google/Vercel or simply set up a entire backend in itself with node/express – these are not solutions to fit this use case.

Neurelo/Hasura/Snap Logic Enterprise plans are extremely expensive for something we had working right now (until sept 25) - Even if its opensource which i think they are not; it requires us to manage the platform they will sit on top of (And pay for that too) -

a lot has been taken from under our feet with little recourse or consideration – A stop gap would be if the mongo team could implement a driver to work with Cloudflare workers as thats the only backend service not describe in your docs.

Hey @Rishi_uttam,

A stop gap would be if the mongo team could implement a driver to work with Cloudflare workers as thats the only backend service not describe in your docs.

We’ve been working with the Cloudflare team for over a year and a half on this (Cloudflare Workers integration is now possible), however due to limitations in the Workers runtime the Node.js driver is unable to connect to Atlas clusters.

I wrote this up in greater detail at Why Cloudflare Workers Don't Work With MongoDB | ALEX BEVILACQUA, however until Cloudflare provides TLS support in their runtime, our drivers can’t be used from that environment.

1 Like

Thank you, I do appreciate the write up, clarifies things. also thanks for keeping the article up to date.