Embark on a journey into the robust backend server architecture of InstaFoodies, where culinary passion meets technological excellence!
Welcome to the Insta Foodies Server repository! This server-side component is an integral part of the Insta Foodies social app, offering food enthusiasts a unique and delightful experience. Powered by Node.js with load balancing and multi-threading capabilities, this server ensures high performance and efficient handling of user traffic. The server integrates seamlessly with the Insta Foodies mobile application, utilizing the Model-View-ViewModel (MVVM) architecture to maintain a well-structured codebase and deliver a smooth user interface.
- System Architecture
- Data Design
- Component Design
- User Authentication and Registration
- Main Feed and Navigation
- User Profile and Settings
- Sharing with Friends, Post Creation and Editing
- AI Models for Food & Spam Detection
- Notify User by Text
- Search Functionality
- Payment Handler
- Chat and Messaging (WhatsApp-like)
- Recipe Web Scraping and Integration
- Multi-Threaded Server and Load Balancing
- Getting Started
- License
Combination of different architectural patterns and concepts to achieve a specific application's goals. While it doesn't precisely fit into any single established pattern, it incorporates several principles to create a custom solution:
Task Queue Architecture:
- This pattern focuses on efficiently distributing tasks among available workers, allowing for parallel processing and scalability.
Microservices-like Separation of Responsibilities:
- Division of components similar to microservices - separation of concerns.
Controller Pattern:
- The controller, processes incoming requests, manages the flow of data, and delegates tasks.
Server-Client Model:
- Clients make requests to the server, which manages the task distribution and worker responses.
Asynchronous Event-Driven Processing:
- The architecture focus on queuing tasks and asynchronous processing is reminiscent of event-driven architectures. Events (tasks) are queued and processed independently when resources are available.
Distributed Processing:
- The delegation of tasks to worker components can be considered a form of distributed processing, distributing computation across multiple units to achieve efficiency.
The server is modularized into several distinct modules, each responsible for specific functionalities:
- User Authentication and Registration: Manages user registration, authentication, and secure processes, including verification emails.
- Post Creation and Editing: Handles post creation, editing, and management, enabling image uploads, descriptions, and structured recipe data.
- Feed Display and Navigation: Displays user feeds with chronological posts and allows interactions such as likes, comments, and shares.
- User Profile and Settings: Manages user profiles, settings, and customization.
- Search Functionality: Implements user search queries, organizes search results, and provides auto-complete suggestions.
- Notifications: Sends notifications for interactions related to user activities and preferences.
- Chat Messaging: Enables real-time chat, individual and group messaging, multimedia sharing, and message history.
- Content Recognition and Reporting: Utilizes AI for object detection to identify recognizable food content in posts, managing reporting and moderation.
- Recipe Web Scraping and Integration: Fetches external recipes, integrates them into the app's database, and displays them seamlessly.
- Multi-Threaded Server and Load Balancing: Manages the application's logic using a multi-threaded server architecture and load balancing through NGINX.
Decomposing the application into modules enhances code readability, maintainability, and scalability. Each module serves a specific purpose, fostering collaboration and allowing new features to be seamlessly integrated.
The server utilizes either Firebase or SQLite as the database to store various types of data relevant to the Insta Foodies app:
- User Information: Stores essential user details, such as usernames, emails, and online/offline status.
- User Account Information: Manages user-specific account data, including followers, followings, cart items, and business/regular distinctions.
- Post Information: Stores comprehensive post data, encompassing captions, ingredients, instructions, images, and timestamps.
- User Feed: Stores references to posts from followed users, contributing to the user's personalized feed.
- User Liked Posts: Tracks references to posts that a user has liked, enhancing the user's engagement with content.
- Reported Posts: Manages references to posts flagged by the content recognition module, facilitating content moderation.
- Utils: Provides a collection for maintaining data such as ingredient options, promoting consistency.
The Insta Foodies Server utilizes the following tables or collections to store data:
- User information - A table or collection that stores the relationship between users.
- User Account information โ A table or collection storing account information.
- Post information - A table or collection that stores posts information.
- User feed - A table or collection storing references to the user's followings posts.
- User-liked posts - A table or collection storing references to posts he/she liked.
- Reported posts โ - A table or collection storing references to MLKIT reported posts.
- Utils โ A table or collection storing Ingredients options list.
The Insta Foodies Server comprises several components, each responsible for specific functionalities:
Component: Login Responsibilities: Manages user registration, login, and authentication. Subcomponents: Login Activity, Layout Login Activity, Register Activity, Layout Register Activity. Functionalities: Register, Login.
Component: Home Responsibilities: Displays the main feed, posts, and comments, and provides user navigation. Subcomponents: Home Activity, Layout Home Activity, Post Adapter, Navigation Menu. Functionalities: Feed view, Post view, like a Post, comment on a post, add post to cart, share post, see more/see less, navigations to Home, Search user, Add Post, Notifications, Profile Page, Chat.
Component: Profile Responsibilities: Manages user profiles, settings, customization, and related data. Subcomponents: Profile Activity, Layout Profile Activity, Edit Profile, Layout Edit Profile. Functionalities: App bar options, profile photo view, post details, user posts grid, cart grid, liked posts grid, navigations to Home, Search user, Add Post, Notifications, Profile Page.
Component: Share Responsibilities: Supports post creation, editing, and sharing with followers. Subcomponents: Share Activity, Image Adapter, Create Post Activity, Layout Share Activity, Layout Create Post Activity. Functionalities: Create post with photos, description, recipe details, navigations to Home, Search user, Add Post, Notifications, Profile Page.
Component: MLKIT Responsibilities: Utilizes AI for food and spam detection, reports illegal post uploads. Subcomponents: Object Detection, Spam Text Detection. Functionalities: Runs AI model on post uploads for content recognition and moderation.
Component: Notifications Responsibilities: Sends notifications to users based on events and preferences. Subcomponents: Reminder, Reminder Broadcast. Functionalities: Sends notifications at specified intervals, reminding users of events.
Component: Search Responsibilities: Implements user search functionality and auto-completion. Subcomponents: Search Activity, Layout Search Activity. Functionalities: Auto-complete users search bar, navigations to Home, Search user, Add Post, Notifications, Profile Page.
Component: Payment Responsibilities: Manages payments to business users for accessing special content. Subcomponents: PayPal Activity. Functionalities: Facilitates payment transactions to business users.
Component: Chat Responsibilities: Enables real-time chat, messaging, and interactions between users. Subcomponents: Chat Activity, Chat Profile Activity, Chat Find Friends Activity, Main Chat Activity, Messages Adapter, Request to Chat Activity. Functionalities: Chat request, accept/reject requests, individual/group chat, multimedia sharing, message history, navigations to Home, Search user, Add Post, Notifications, Profile Page.
Component: Scraping Responsibilities: Fetches and integrates external recipes into the app's database. Subcomponents: Scraping file. Functionalities: Retrieves recipes from external sources and integrates them seamlessly.
Component: Insta-foodies Server Responsibilities: Manages multi-threaded server architecture and load balancing. Subcomponents: Server, Workers Handler, Worker, Entry Controller. Functionalities: Efficiently handles incoming requests, balances workloads, and ensures optimal performance.
You can follow this step-by-step guide or watch the video tutorial on YouTube.
-
Install npm:
npm install -g npm
-
Install Node.js (version 18.5.0) on your computer:
- Windows: Download Node.js
- Ubuntu:
sudo npm cache clean -f sudo npm install -g n sudo n latest
-
Open a folder for the server.
-
Clone the project to the folder.
-
Navigate to the project folder in the terminal.
-
Initialize the project:
npm init
-
Install dependencies:
npm install express firebase-auth firebase firebase-admin firebase-tools nodemon morgan apicache
- To keep the server running and automatically update with changes, add the following to your
package.json
:"scripts": { "start": "nodemon server.js", ... }
- To keep the server running and automatically update with changes, add the following to your
-
Generate a new private key for Firebase:
- Go to Project settings -> Service accounts.
- Generate a new private key.
- Open the
key.js
file and paste the generated key.
-
Install Nginx (web server similar to Apache):
-
Using Nginx as a load balancer:
- Watch the tutorial
- General commands:
- Check Nginx configuration:
nginx -t
- Start Nginx:
start nginx
- Reload Nginx:
nginx -s reload
- Check Nginx configuration:
-
Install OpenSSL on Windows: Tutorial
-
To run the server with Nodemon:
npm start
-
To run the server without Nodemon:
node .\server.js
- For more details, watch this video
-
Install PM2 (process manager):
npm install -g pm2
-
Start the server with PM2:
pm2 start server.js
-
Additional commands:
pm2 status pm2 restart server.js pm2 stop server.js pm2 logs pm2 flush
-
To run multiple servers on different ports:
- Install cross-env:
npm install --save-dev cross-env
- Start servers with different ports:
npx cross-env PORT=3000 pm2 start --name server1 server.js
- Install cross-env:
-
Save PM2 configuration:
pm2 save
This script: powershell -ExecutionPolicy Bypass
allows running scripts without permanently changing the system's execution policy.
nginx -t
start nginx
Start Nginx Load Balancer |
---|
npx cross-env PORT=3000 pm2 start --name server1 server.js
Server Creation With Pm2 |
---|
npx cross-env pm2 list
Servers Are Running && Waiting For Tasks |
---|
- Building the Android Side (Client) Tutorial
- Retrofit Documentation
- Organizing Files in Node.js
- CRUD Methods Tutorial
- Retrofit Tutorials
- HTTP Status Codes
- Guide on Running Multiple Servers with PM2
Once the server is set up and running, it seamlessly integrates with the Insta Foodies mobile application. Users can enjoy the unique and delightful experience of connecting with fellow food enthusiasts, exploring recipes, sharing content, and engaging in real-time chat.
This project is licensed under the Creative Commons Attribution-NonCommercial 4.0 International License (CC BY-NC 4.0). This means that you are free to share, adapt, and build upon the material, as long as you provide appropriate attribution, do not use the material for commercial purposes, and do not impose additional legal restrictions.
Please note that this license is designed to prevent commercial usage of the code. If you have any questions about how you can use or adapt this code within the terms of the license, feel free to contact us via email smartefoodies@gmail.com ๐ฎ.
If you have any questions, suggestions, or feedback, please don't hesitate to contact us:
smarterfoodies@gmail.com ๐ฎ.