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