A Node.js backend application built with TypeScript following Hexagonal Architecture (Ports and Adapters) pattern. The application handles real-time event publishing using Redis Pub/Sub and Server-Sent Events (SSE).
This project implements Hexagonal Architecture (also known as Ports and Adapters pattern) which divides the application into three main layers:
The innermost layer containing business logic and domain entities:
domain/entities: Core business entities (e.g.,IUser)domain/dtos: Data Transfer Objects for domain entities
The middle layer that orchestrates the flow of data and implements use cases:
application/ports: Interfaces that define how the application interacts with external servicesapplication/usecases: Implementation of business use casesapplication/validators: Validation rules for input data
The outermost layer that handles external concerns:
infrastructure/adapters/input: Handles incoming requests (HTTP routes, SSE)infrastructure/adapters/output: Implements data persistence (MongoDB repositories)infrastructure/config: Application configurationinfrastructure/services: External service implementations (Redis, JWT)
Here's how the application flows from startup to handling requests:
-
Application Bootstrap (
index.ts):ApplicationBootstrap.start() └── Application instance created ├── Services initialized │ ├── MongoDB connection │ ├── Redis connection │ └── SSE service └── Express server started
-
Request Handling Flow:
Client Request └── Infrastructure Layer (Express Routes) └── Application Layer (Use Cases) └── Domain Layer (Business Logic) └── Infrastructure Layer (Repositories) └── Response -
Real-time Updates Flow:
Event Trigger └── Redis Publisher └── Redis Subscriber └── SSE Service └── Client Browser
Handles pub/sub messaging for real-time communication:
interface IRedisService {
publish(channel: string, message: any): Promise<void>;
subscribe(channel: string, callback: Function): Promise<void>;
unsubscribe(channel: string): Promise<void>;
}Manages Server-Sent Events connections:
interface ISSEService {
initializeConnection(username: string, res: Response): void;
closeConnection(username: string): void;
broadcastToUsers(usernames: string[], data: any): Promise<void>;
}Implements multiple strategies using Passport.js:
- Local authentication with JWT
- Google OAuth2.0
- Session management
- Runtime: Node.js
- Framework: Express.js with TypeScript
- Database: MongoDB
- Caching & Pub/Sub: Redis
- Authentication: Passport.js
- Real-time: Server-Sent Events (SSE)
- Architecture: Hexagonal (Ports & Adapters)
-
Clone the repository:
git clone https://github.com/yourusername/event-pub-backend.git
-
Install dependencies:
npm install
-
Create
.envfile:PORT=4000 NODE_ENV=development MONGO_URL=mongodb://localhost:27017 MONGO_DB_NAME=eventdb REDIS_URL=redis://localhost:6379 JWT_SECRET=your_jwt_secret
-
Start the development server:
npm run dev
event-pub-backend/
├── src/
│ ├── application/ # Application business rules
│ │ ├── ports/ # Interface definitions
│ │ ├── usecases/ # Use case implementations
│ │ └── validators/ # Input validation
│ ├── domain/ # Enterprise business rules
│ │ ├── dtos/ # Data Transfer Objects
│ │ └── entities/ # Domain entities
│ ├── infrastructure/ # Frameworks & tools
│ │ ├── adapters/ # Interface adapters
│ │ ├── config/ # Configurations
│ │ ├── database/ # Database setup
│ │ ├── middleware/ # Express middleware
│ │ └── services/ # External services
│ └── shared/ # Shared utilities
│ ├── errors/ # Error handling
│ └── utils/ # Utility functions
- JWT-based authentication
- HTTP-only cookies
- CORS configuration
- Input validation
- Error handling middleware
- Secure session configuration
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.