I have a NextJS project I'm working on and I guess I'm not sure what flow is considered best-practice.
For context - most of the tutorials I've watched have used plain ReactJS or ViteJS so it's all frontend (auth, fetching data, sending data, realtime subscriptions, etc).
Since I am using NextJS I have access to a backend API as well, and my plan was to use the backend API to be the middleman between the frontend and appwrite. This makes sense for my auth because I have some additional validation I want to run before attempting to contact appwrite to authenticate the credentials. But for the other stuff like if a user sends a message, I don't really have any additional validation for it so I think it's fine to not bother with a middleman and just have the frontend send the message to appwrite directly. This also makes sense if I wanted to use the real time subscription for that collection. I don't think it's possible for me to have real time capabilities if I send and receive messages via a backend API first.
So is it common to only have a backend API for actions I need to perform additional validations for but for the rest, it will be the frontend and appwrite communicating with each other directly?
Amm yes, you can use backend API as a middleman. However, for actions that don't require such processing and are straightforward, it is perfectly acceptable to have the frontend communicate directly with Appwrite.
Here's an idea:
- Backend API for Additional Validation:
- For actions like authentication or sensitive operations that require additional validation, using a backend API as a middleman is beneficial. The backend can handle user authentication, input validation, and other business logic before communicating with Appwrite.
- Frontend Direct Communication with Appwrite:
- For simpler operations that do not require additional validation or server-side processing, it is entirely acceptable to have the frontend communicate directly with Appwrite. Actions like fetching data, sending messages, or retrieving non-sensitive information can be directly handled by the frontend, interacting with Appwrite's APIs.
- Real-Time Capabilities:
- As you mentioned, if you want to utilize real-time capabilities, direct communication between the frontend and Appwrite becomes necessary. This is because real-time features often require WebSocket connections, and having a backend API as a middleman can introduce additional latency and complexity.
- Optimizing Performance:
- Direct communication between the frontend and Appwrite can also help optimize performance by reducing the number of hops and potential bottlenecks in the communication flow.
An example how to do backend (SSR) auth, and then backend (SSR), or frontend (browser) account.get(): https://next-js.ssr.almostapps.eu/
Source code: https://github.com/Meldiron/appwrite-ssr-next-js
Also SSR is beneficial for SEO
Thank you so very much for the detailed guide. I guess I'm glad that my plan is on the right path. Thanks to the appwrite team! Amazing product and amazing support too!
Recommended threads
- Update User Error
```ts const { users, databases } = await createAdminClient(); const session = await getLoggedInUser(); const user = await users.get(session.$id); if (!use...
- apple exchange code to token
hello guys, im new here 🙂 I have created a project and enabled apple oauth, filled all data (client id, key id, p8 file itself etc). I generate oauth code form...
- I recently applied for the free plan und...
I recently applied for the free plan under the GitHub Student Developer Pack. However, my billing status still shows $15, and it mentions that this amount will ...