A modern real-time chat application backend built with NestJS, Oracle Database, and WebSocket integration. This project features guild-based chat rooms, user authentication via Keycloak, and a robust database architecture with Oracle PL/SQL packages.
- π Features
- π οΈ Tech Stack
- π Prerequisites
- β‘ Quick Start
- ποΈ Project Structure
- π API Endpoints
- ποΈ Database Schema
- π Available Scripts
- π³ Docker Configuration
- π Authentication Setup (Keycloak)
- π οΈ Troubleshooting
- Real-time Communication: WebSocket integration for instant messaging
- Guild-based Architecture: Discord-like guild and channel management
- Authentication: Keycloak integration for secure user authentication
- Oracle Database: Enterprise-grade database with PL/SQL packages and procedures
- RESTful API: Complete CRUD operations for users, guilds, and channels
- Type Safety: Full TypeScript implementation with strict typing
- Validation: Input validation using class-validator
- Docker Support: Complete containerization with Docker Compose
- Kubernetes Support: Minikube setup for local Kubernetes cluster deployment
- Backend Framework: NestJS with Fastify
- Database: Oracle 18c Express Edition
- Authentication: Keycloak with PostgreSQL
- Language: TypeScript
- WebSockets: Socket.IO
- Validation: class-validator, class-transformer
- Containerization: Docker & Docker Compose (+ Kubernetes setup)
- Package Manager: pnpm
Before running this project, make sure you have the following installed:
- Node.js >= 20.X.X
- npm >= 10.X.X (or pnpm recommended)
- Docker and Docker Compose
- Git
git clone <repository-url>
cd Fidechat-backend# Using pnpm (recommended)
pnpm install
# Or using npm
npm installCreate a .env file in the root directory with the following variables:
# Application
NODE_ENV=development
API_PORT=3000
# Oracle Database
ORACLE_HOST=localhost
ORACLE_PORT=1521
ORACLE_USER=system
ORACLE_PWD=your_oracle_password
ORACLE_SERVICE_NAME=XEPDB1
# PostgreSQL (for Keycloak)
PG_USER=keycloak
PG_PASSWORD=your_postgres_password
PG_NAME=keycloak
DB_PORT=5432
# Keycloak
KEYCLOAK_ADMIN=admin
KEYCLOAK_ADMIN_PASSWORD=your_keycloak_admin_password
KC_PORT=8081
KEYCLOAK_URL=http://localhost:8081
KEYCLOAK_REALM=your_realm
KEYCLOAK_CLIENT_ID=your_client_id
KEYCLOAK_CLIENT_SECRET=your_client_secret
# Keycloak Database
KC_DB_USERNAME=keycloak
KC_DB_PASSWORD=your_postgres_password
KC_DB_URL=jdbc:postgresql://postgres:5432/keycloak# Start all services (Oracle, PostgreSQL, Keycloak)
docker-compose -f local-docker-compose.yaml up -d
# Wait for services to be ready (especially Oracle DB initialization)
# This may take a few minutes on first runThe Oracle database will be automatically initialized with the schema and packages from /sql/init.sql when the container starts for the first time.
# Development mode with hot reload
pnpm run dev
# Or using npm
npm run dev
# Production mode
pnpm run start:prodThe application will be available at:
- API: http://localhost:3000
- Keycloak Admin: http://localhost:8081 (admin/your_keycloak_admin_password)
.
βββ scripts # Helper scripts for project setup and maintenance
βββ sql # Database scripts
β βββ init.sql # Main initialization script
β βββ pkg_*.sql # PL/SQL packages
β βββ triggers.sql
βββ src # Application source code
β βββ common # Shared utilities and services
β β βββ logger # Custom logging implementation
β β β βββ constants # Logger constants
β β β βββ interfaces # Logger interfaces
β β β βββ utils # Logger utilities
β β βββ utils # Environment validation
β βββ config # Configuration files
β βββ database # Database connection and types
β β βββ oracle # Oracle-specific implementations
β β βββ query-builder # Query builder for Oracle
β β βββ types # Oracle-specific types
β βββ modules # Feature modules
β β βββ auth # Authentication (Keycloak)
β β β βββ keycloak
β β βββ channel
β β βββ dashboard
β β βββ gateway
β β βββ guild
β β βββ user # User management
β β βββ currentUser # Current user utilities (like sync data client <- backend)
β βββ utils # Utility functions
βββ types # Global TypeScript types
todo
- Authentication is handled via Keycloak JWT tokens
- Include
Authorization: Bearer <token>header in requests
GET /user/sync- Get current user sync data- User CRUD operations are handled via PL/SQL packages
POST /guild- Create a new guildGET /guild/:id- Get guild by IDPUT /guild/:id- Update guildDELETE /guild/:id- Delete guild
POST /channel- Create a new channelGET /channel/:id- Get channel by IDPUT /channel/:id- Update channelDELETE /channel/:id- Delete channel
GUILD_CREATE- Guild creation eventsCHANNEL_CREATE- Channel creation events- Real-time updates for all guild and channel operations
The application uses Oracle Database with the following main entities:
- APP_USER: User information and profiles
- GUILD: Chat server/guild data
- CHANNEL: Text channels within guilds
- GUILD_USERS: Many-to-many relationship for guild membership
Key PL/SQL packages:
PKG_USER: User management operationsPKG_GUILD: Guild management operationspkg_sync_data: Data synchronization functions
# Development
pnpm run dev # Start development server with hot reload
pnpm run start:debug # Start with debug mode
# Building
pnpm run build # Build the application
pnpm run start:prod # Start production server
# Code Quality
pnpm run lint # Run ESLint
pnpm run format # Format code with PrettierThe project includes a complete Docker setup:
- Oracle 18c Express: Main application database
- PostgreSQL: Keycloak database
- Keycloak: Authentication and authorization server
# Start all services
docker-compose -f local-docker-compose.yaml up -d
# View logs
docker-compose -f local-docker-compose.yaml logs -f- Access Keycloak Admin Console at http://localhost:8081
- Login with admin credentials from your
.envfile - Create a new realm or configure the existing one
- Create a client for the application
- Update the
.envfile with the client credentials
-
Oracle Connection Issues
- Ensure Oracle container is fully started (may take 2-3 minutes)
- Check Oracle credentials in
.envfile - Verify ORACLE_SERVICE_NAME is correct
-
Keycloak Authentication
- Verify Keycloak is accessible at the configured URL
- Check client configuration in Keycloak admin
- Ensure JWT tokens are properly formatted
-
WebSocket Connection Issues
- Check CORS configuration
- Pray to Jesus if all else fails
- Fix socket connection TTL: Currently, using Keycloak, there is no mechanism to disconnect clients with expired Keycloak tokens. Implement a solution to refresh tokens or disconnect clients when their tokens expire.
- Implement missing CRUD operations in sockets.
