Skip to content

ZeroToH3ro/social-media-api

Repository files navigation

Nest Logo

Social Media API

A full-stack social media API built with NestJS, supporting MySQL for core data and MongoDB for notifications, with real-time notification delivery using WebSockets.


Features

  • User authentication & authorization
  • Posts, comments, likes, follows, tags, and profiles
  • File uploads with Cloudinary
  • Notifications stored in MongoDB
  • Real-time notifications via WebSockets (Socket.IO)
  • Message queuing with Bull/Redis
  • Modular, scalable codebase

Tech Stack

  • Backend: NestJS, TypeORM
  • Databases: MySQL (main data), MongoDB (notifications)
  • Real-time: WebSockets (Socket.IO)
  • Queue System: Bull/Redis
  • Cloud Storage: Cloudinary

Project Setup

Traditional Setup

pnpm install

Docker Setup (Recommended)

The project includes Docker configuration for easy setup and consistent environments.

  1. Prerequisites:

  2. Starting the application:

# Start all services
docker-compose up

# Run in background
docker-compose up -d

# Build and start (after code changes)
docker-compose up --build

This will start the following services:

  • NestJS application (API)
  • MySQL database
  • MongoDB
  • Redis (for Bull queue)
  • NATS (message broker)
  1. Stopping the application:
docker-compose down

# To remove volumes as well (will delete data)
docker-compose down -v

Environment Variables

Create a .env file in the root directory:

# MySQL Database
DB_USERNAME=your_mysql_user
DB_PASSWORD=your_mysql_password
DB_NAME=social_media
DB_HOST=localhost

# MongoDB Database
MG_DBNAME=social_media_mongo
MG_HOST=localhost

# Cloudinary
CLOUDINARY_CLOUD_NAME=your_cloud_name
CLOUDINARY_API_KEY=your_api_key
CLOUDINARY_API_SECRET=your_api_secret

# Redis (for Bull Queue)
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=

# Bull Board Admin
ADMIN_USER=admin
ADMIN_PASSWORD=securepassword

When using Docker, the database hosts will be the service names:

DB_HOST=mysql
MG_HOST=mongodb
REDIS_HOST=redis

Running the Project

Without Docker

# development
pnpm run start

# watch mode
pnpm run start:dev

# production mode
pnpm run start:prod

With Docker

# Development mode
docker-compose up

Accessing Services


Real-Time Notifications

  • The API uses Socket.IO for real-time notifications.
  • Clients should connect with authentication:
import { io } from "socket.io-client";
const socket = io('http://localhost:3000/notifications', {
  auth: { token: 'YOUR_JWT_TOKEN' }
});

socket.on('notification', (notification) => {
  // handle new notification
});

socket.on('unread_count', (data) => {
  // update notification badge with data.count
});

Docker Commands

# View container logs
docker-compose logs -f api

# Access shell in container
docker-compose exec api sh

# Run tests
docker-compose exec api pnpm test

# Rebuild a specific service
docker-compose up -d --no-deps --build api

Testing

# unit tests
pnpm run test

# e2e tests
pnpm run test:e2e

# test coverage
pnpm run test:cov

Deployment

Docker-based Deployment

  1. Configure production environment variables
  2. Run with production compose file:
docker-compose -f docker-compose.prod.yml up -d

For cloud deployments, see NestJS deployment docs.


Resources


License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published