Skip to content

soufianehamdach28/social-network

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Full Stack Social Network (NestJS + Next.js + PostgreSQL)

NestJS Next.js PostgreSQL Docker Redis


πŸ“Œ 1. Project Overview

This project is a full-featured social network application similar to Facebook or Instagram.

πŸ‘‰ Users can:

  • Create accounts (Public or Private)
  • Share posts (with images and privacy settings)
  • Follow other users (Accept/Decline requests)
  • Chat in real-time (Private and Groups)
  • Join communities and groups
  • Receive real-time notifications

🧠 2. How the System Works (Big Picture)

The system is built with a modern, scalable architecture divided into 3 main parts:

1. Frontend (Next.js)

πŸ‘‰ What the user sees and interacts with. Built with React and Tailwind CSS.

2. Backend (NestJS)

πŸ‘‰ The brain of the application (logic + REST API + WebSockets).

3. Database (PostgreSQL)

πŸ‘‰ Relational database that stores all data safely and efficiently.

πŸ”„ Request Flow Example

  1. User clicks "Create Post"
  2. Frontend sends an HTTP request to the backend
  3. Backend processes the request and validates data
  4. Data is saved in the PostgreSQL database
  5. Backend sends a success response
  6. Frontend updates the UI

🧱 3. Tech Stack

Backend

  • NestJS (Node.js framework)
  • WebSockets (Real-time features via NestJS Gateway)
  • REST API (with secure routes and middleware)

Frontend

  • Next.js (React framework - App Router)
  • Tailwind CSS (for styling)
  • Fetch API / Axios (for data fetching)
  • React Context / Zustand (for state management)

Database

  • PostgreSQL (Relational Database)

βš™οΈ 4. Backend Explanation (NestJS)

The backend is modular. Each feature is isolated into its own module for scalability and maintainability.

πŸ“ Structure

src/
β”œβ”€β”€ modules/
β”‚   β”œβ”€β”€ auth/          # Login & registration (Sessions/Cookies, bcrypt)
β”‚   β”œβ”€β”€ users/         # Profiles, public/private accounts
β”‚   β”œβ”€β”€ posts/         # Feed, posts, comments, likes
β”‚   β”œβ”€β”€ follows/       # Follow graphs, requests
β”‚   β”œβ”€β”€ groups/        # Communities, group posts, events
β”‚   β”œβ”€β”€ chat/          # Real-time WebSocket messaging
β”‚   β”œβ”€β”€ notifications/ # Real-time alerts
β”‚   β”œβ”€β”€ feed/          # Smart feed ranking algorithm
β”‚   └── search/        # Full-text PostgreSQL search
β”œβ”€β”€ common/            # Guards, interceptors, filters
β”œβ”€β”€ database/          # Entities and migrations
└── main.ts

πŸ” Core Modules Breakdown

  • Auth Module: Handles sessions, cookies, and password hashing (bcrypt). Endpoints include /auth/register, /auth/login, and /auth/logout.
  • Posts Module: Core platform feature allowing image uploads, privacy toggles, and managing likes/comments.
  • Chat Module: Uses WebSockets for real-time private/group messages, typing indicators, and online status.
  • Groups Module: Allows creating communities, joining via invites, making group posts, and scheduling events.

πŸ—„οΈ 5. Database Design (PostgreSQL)

The database schema is highly relational, connecting users, their content, and interactions.

πŸ‘€ Users & Social Graph

  • users: id, email, password_hash, first_last_name, avatar_url, is_private, is_online, last_seen
  • sessions: Tracks active login tokens and expirations.
  • follows: follower_id, following_id
  • follow_requests: Manages pending follow requests for private profiles.
  • blocked_users: Social privacy controls.

πŸ“ Posts & Interactions

  • posts: id, user_id, content, image_url, privacy, likes_count, comments_count
  • comments: Allows threaded replies via parent_id.
  • likes: Connects users to liked posts.
  • saved_posts: Bookmarked content.

πŸ‘₯ Groups & Events

  • groups: id, creator_id, title, description
  • group_members: Manages roles (admin/member) and statuses.
  • group_posts & events: Community content and scheduled events (with event_responses).

πŸ’¬ Chat & Notifications

  • conversations & messages: Tracks sender, receiver, and read receipts (sent/delivered/seen).
  • notifications: Triggers alerts (type, reference_id, is_read).
  • activities: Advanced tracking for analytics.

🎨 6. Frontend Explanation (Next.js)

The frontend uses Next.js App Router to organize pages clearly.

πŸ“ Structure & Pages

app/
β”œβ”€β”€ (auth)/        # Login and Register pages
β”œβ”€β”€ feed/          # Smart feed with popular posts and create post UI
β”œβ”€β”€ profile/       # User profile, posts, followers/following lists
β”œβ”€β”€ messages/      # Real-time chat interface
β”œβ”€β”€ groups/        # Browse communities and view events
β”œβ”€β”€ notifications/ # Live action alerts

⚑ 7. Advanced Features Explained

  • 🧠 Smart Feed: Ranks posts by engagement (likes Γ— 2) + comments + recency rather than just chronological order.
  • πŸ”Ž Search Engine: Uses PostgreSQL full-text search to find people, posts, and groups quickly.
  • πŸ’¬ Real-Time System: Instantly delivers messages, typing indicators, read receipts, and handles "Online Status" via WebSockets.
  • πŸ” Advanced Privacy: Granular controls for post visibility, blocking users, and setting entire accounts to "private".
  • πŸ“‹ Moderation & Analytics: Built-in reporting system for toxic content and activity tracking to understand user behavior.

🐳 8. Docker Environment

Docker ensures the application runs consistently across any environment using isolated containers:

  1. Backend Container: Hosts the NestJS server.
  2. Database Container: Hosts the PostgreSQL server.
  3. Frontend Container: Hosts the Next.js React app.

🎯 9. Development Roadmap

  1. Authentication & Users
  2. Posts & Follows
  3. Chat & Notifications
  4. Groups & Events
  5. Advanced Features (Feed, Search, Analytics)

πŸ’‘ 10. Conclusion

This architecture is scalable, maintainable, and strictly follows industry standards. By completing this, you implement core full-stack features: scalable database schema design, real-time connection management, security, and smart algorithms.

πŸ”₯ This is a production-ready architecture.


πŸš€ 11. Quick Start (Run Locally)

The easiest way to run the entire application is using Docker. The provided docker-compose.yml sets up the Database, Redis, MinIO, Backend, and Frontend automatically.

Prerequisites

  • Docker & Docker Compose installed on your machine.
  • Git to clone the repository.

Installation Steps

  1. Clone the repository

    git clone https://github.com/soufianehamdach28/social-network.git
    cd social-network
  2. Start the application with Docker Compose

    docker-compose up -d
  3. Access the Application

Note: The backend container automatically installs dependencies, generates the Prisma client, and applies database migrations upon startup.


🀝 12. Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ 13. License

Distributed under the MIT License. See LICENSE for more information.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors