{"id":46463874,"url":"https://github.com/ndum/livechat-backend","last_synced_at":"2026-03-06T04:03:50.763Z","repository":{"id":212847572,"uuid":"704570394","full_name":"ndum/livechat-backend","owner":"ndum","description":"A modern, scalable live chat backend built with TypeScript, Express, Prisma, and MongoDB. Features real-time communication via WebSockets, JWT authentication, and comprehensive API documentation.","archived":false,"fork":false,"pushed_at":"2026-01-02T15:31:42.000Z","size":274,"stargazers_count":0,"open_issues_count":0,"forks_count":5,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-08T21:24:24.456Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ndum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-10-13T14:41:21.000Z","updated_at":"2026-01-02T15:30:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"9e201ed6-5ca9-4d1a-ab4a-0bef1e4a2564","html_url":"https://github.com/ndum/livechat-backend","commit_stats":null,"previous_names":["ndum/livechat-backend"],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/ndum/livechat-backend","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndum%2Flivechat-backend","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndum%2Flivechat-backend/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndum%2Flivechat-backend/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndum%2Flivechat-backend/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ndum","download_url":"https://codeload.github.com/ndum/livechat-backend/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ndum%2Flivechat-backend/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30161345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-05T22:39:40.138Z","status":"online","status_checked_at":"2026-03-06T02:00:08.268Z","response_time":250,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-03-06T04:03:01.875Z","updated_at":"2026-03-06T04:03:50.751Z","avatar_url":"https://github.com/ndum.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Live Chat Backend\n\nA modern, scalable live chat backend built with TypeScript, Express, Prisma, and MongoDB. Features real-time communication via WebSockets, JWT authentication, and comprehensive API documentation.\n\n\u003e This project was primarily created for the \"WEB I\" course at HF-Informatik.\n\n## Features\n\n- **Real-time Communication**: WebSocket support for instant message delivery\n- **Secure Authentication**: JWT-based authentication with password hashing\n- **Type-Safe**: Full TypeScript implementation with Prisma ORM\n- **Clean Architecture**: Layered architecture (Controllers \u003e Services \u003e Repositories)\n- **API Documentation**: Interactive Swagger/OpenAPI documentation\n- **Comprehensive Testing**: Full test coverage with Vitest\n- **Security**: Rate limiting, helmet.js, input validation with Zod\n- **Database**: MongoDB with Prisma ORM\n\n## Tech Stack\n\n- **Runtime**: Node.js\n- **Language**: TypeScript\n- **Framework**: Express.js\n- **Database**: MongoDB (with replica set support)\n- **ORM**: Prisma\n- **Authentication**: JWT (jsonwebtoken)\n- **Validation**: Zod\n- **Testing**: Vitest + Supertest\n- **Documentation**: Swagger/OpenAPI\n- **Logging**: Pino\n- **WebSockets**: ws (WebSocket library)\n- **Code Quality**: ESLint + Prettier\n\n## Prerequisites\n\n- Node.js \u003e= 22\n- MongoDB \u003e= 7.0 (configured as replica set)\n- npm or yarn\n\n## Installation\n\n1. Clone the repository:\n```bash\ngit clone \u003crepository-url\u003e\ncd livechat-backend\n```\n\n2. Install dependencies:\n```bash\nnpm install\n```\n\n3. Configure environment variables:\n```bash\ncp .env.example .env\n```\n\nEdit `.env` with your configuration (see Environment Variables section below).\n\n4. Set up MongoDB replica set (required for Prisma transactions):\n\n**Windows:**\n- Edit `mongod.cfg` (usually in `C:\\Program Files\\MongoDB\\Server\\\u003cversion\u003e\\bin\\`)\n- Add:\n  ```yaml\n  replication:\n    replSetName: \"rs0\"\n  ```\n- Restart MongoDB service\n- Initialize replica set:\n  ```bash\n  mongosh\n  rs.initiate()\n  ```\n\n**Linux/Mac:**\n- Add to your MongoDB config:\n  ```yaml\n  replication:\n    replSetName: \"rs0\"\n  ```\n- Restart MongoDB\n- Initialize: `mongosh` then `rs.initiate()`\n\n5. Generate Prisma client:\n```bash\nnpx prisma generate\n```\n\n6. (Optional) Seed the database:\n```bash\nnpm run seed\n```\n\n## Environment Variables\n\nCreate a `.env` file in the project root with the following variables:\n\n```env\n# Server Configuration\nNODE_ENV=development\nPORT=3000\nURL=http://localhost\nBEHIND_PROXY=false\n\n# Database\nDATABASE_URL=mongodb://localhost:27017/livechat\n\n# JWT Secret (generate a secure random string)\nJWT_SECRET=your-super-secret-jwt-key-change-this-in-production\n\n# Docker\nDOCKER=false\n```\n\n**Important**: Set `BEHIND_PROXY=true` if running behind a reverse proxy (nginx, Apache, etc.)\n\n## Running the Application\n\n### Development Mode\n```bash\nnpm run dev\n```\n\nServer will start at `http://localhost:3000`\n\n### Production Mode\n```bash\nnpm run build\nnpm start\n```\n\n## Reverse Proxy Configuration\n\nWhen running behind a reverse proxy (e.g., nginx, Apache), ensure WebSocket support is enabled.\n\n### Example nginx Configuration\n\n```nginx\nserver {\n    listen 80;\n    server_name yourdomain.com;\n\n    location / {\n        proxy_pass http://localhost:3000;\n        proxy_http_version 1.1;\n\n        # WebSocket support\n        proxy_set_header Upgrade $http_upgrade;\n        proxy_set_header Connection \"upgrade\";\n\n        # Standard proxy headers\n        proxy_set_header Host $host;\n        proxy_set_header X-Real-IP $remote_addr;\n        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n        proxy_set_header X-Forwarded-Proto $scheme;\n    }\n}\n```\n\n**Important**: Set `BEHIND_PROXY=true` in your `.env` file when using a reverse proxy.\n\n## Testing\n\nRun all tests:\n```bash\nnpm test\n```\n\nRun tests with coverage:\n```bash\nnpm run test:coverage\n```\n\nRun tests in watch mode:\n```bash\nnpm run test:watch\n```\n\n## API Documentation\n\nOnce the server is running, access the interactive Swagger documentation at:\n\n```\nhttp://localhost:3000/api/docs\n```\n\n### API Endpoints\n\n**Authentication:**\n- `POST /api/v1/auth/register` - Register a new user\n- `POST /api/v1/auth/login` - Login and receive JWT token\n- `POST /api/v1/auth/logout` - Logout user\n\n**Users:**\n- `GET /api/v1/users` - Get all users (public)\n- `GET /api/v1/users/:id` - Get user by ID (public)\n- `PUT /api/v1/users/:id` - Update user profile (authenticated, own profile only)\n- `DELETE /api/v1/users/:id` - Delete user account (authenticated, own account only)\n\n**Chat Messages:**\n- `GET /api/v1/messages` - Get all messages (public)\n- `GET /api/v1/messages/:id` - Get message by ID (public)\n- `POST /api/v1/messages` - Create new message (authenticated)\n- `PUT /api/v1/messages/:id` - Update message (authenticated, own message only)\n- `DELETE /api/v1/messages/:id` - Delete message (authenticated, own message only)\n\n### WebSocket Events\n\nConnect to WebSocket at `ws://localhost:3000`\n\n**Events broadcast to all clients:**\n- `new_login` - User logged in\n- `new_message` - New chat message created\n- `changed_message` - Message updated\n- `deleted_message` - Message deleted\n- `changed_user` - User profile updated\n- `deleted_user` - User account deleted\n- `new_logout` - User logged out\n\n## Project Structure\n\n```\nlivechat-backend/\n├── prisma/\n│   └── schema.prisma          # Prisma database schema\n├── src/\n│   ├── config/                # Configuration files\n│   │   ├── env.ts            # Environment validation\n│   │   ├── logger.ts         # Pino logger setup\n│   │   └── swagger.ts        # Swagger/OpenAPI config\n│   ├── controllers/           # Request handlers\n│   │   ├── auth.controller.ts\n│   │   ├── chat.controller.ts\n│   │   └── user.controller.ts\n│   ├── infrastructure/        # External services\n│   │   ├── database.ts       # Prisma client singleton\n│   │   └── websocket.ts      # WebSocket server setup\n│   ├── middleware/            # Express middleware\n│   │   ├── auth.ts           # JWT authentication\n│   │   ├── errorHandler.ts   # Global error handler\n│   │   └── validate.ts       # Zod validation middleware\n│   ├── repositories/          # Data access layer\n│   │   ├── chatMessage.repository.ts\n│   │   └── user.repository.ts\n│   ├── routes/                # API routes\n│   │   └── v1/\n│   │       ├── index.ts\n│   │       ├── auth.routes.ts\n│   │       ├── chat.routes.ts\n│   │       └── user.routes.ts\n│   ├── services/              # Business logic\n│   │   ├── auth.service.ts\n│   │   ├── chat.service.ts\n│   │   └── user.service.ts\n│   ├── tests/                 # Test files\n│   │   ├── setup.ts\n│   │   ├── helpers.ts\n│   │   ├── auth.test.ts\n│   │   ├── chat.test.ts\n│   │   └── user.test.ts\n│   ├── utils/                 # Utility functions\n│   │   └── errors.ts         # Custom error classes\n│   ├── validators/            # Zod schemas\n│   │   ├── auth.validator.ts\n│   │   ├── chat.validator.ts\n│   │   └── user.validator.ts\n│   └── server.ts             # Application entry point\n├── .env.example              # Environment variables template\n├── package.json\n├── tsconfig.json\n└── vitest.config.ts          # Test configuration\n```\n\n## Architecture\n\nThis project follows clean architecture principles with clear separation of concerns:\n\n1. **Controllers**: Handle HTTP requests/responses, delegate to services\n2. **Services**: Contain business logic, orchestrate repositories\n3. **Repositories**: Data access layer, interact with database via Prisma\n4. **Validators**: Input validation using Zod schemas\n5. **Middleware**: Cross-cutting concerns (auth, validation, error handling)\n\n## Security Features\n\n- **Password Hashing**: bcrypt with salt rounds\n- **JWT Tokens**: Secure token-based authentication (1-hour expiration)\n- **Rate Limiting**: Protection against brute force attacks\n- **Input Validation**: Zod schemas validate all inputs\n- **Helmet.js**: Security headers\n- **CORS**: Configurable cross-origin resource sharing\n- **Ownership Validation**: Users can only modify their own resources\n\n## Development\n\n### Available Scripts\n\n```bash\nnpm run dev              # Start development server with hot reload\nnpm run build            # Build for production\nnpm start                # Start production server\nnpm test                 # Run tests\nnpm run test:coverage    # Run tests with coverage\nnpm run lint             # Check code with ESLint\nnpm run lint:fix         # Auto-fix ESLint issues\nnpm run format           # Format code with Prettier\nnpm run format:check     # Check code formatting\nnpm run typecheck        # Type check with TypeScript\n```\n\n### Database Commands\n\nView database in Prisma Studio:\n```bash\nnpx prisma studio\n```\n\nCreate a new migration:\n```bash\nnpx prisma migrate dev --name migration-name\n```\n\nReset database:\n```bash\nnpx prisma migrate reset\n```\n\n### Docker\n\nBuild and run with Docker:\n```bash\ndocker build -t livechat-backend .\ndocker run -p 3000:3000 -e DATABASE_URL=\"mongodb://host:27017/livechat\" livechat-backend\n```\n\nOr use Docker Compose:\n```bash\ndocker-compose up --build\n```\n\n### Code Quality\n\nThe codebase follows these principles:\n- Self-documenting code with clear naming\n- JSDoc comments for public APIs\n- Minimal comments (explain \"why\", not \"what\")\n- TypeScript strict mode\n- Comprehensive error handling\n- Full test coverage\n\n## Contributing\n\n1. Fork the repository\n2. Create a feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## License\n\nThis project is licensed under the MIT License.\n\n## Author\n\nNicolas Dumermuth\n\n## Support\n\nFor issues and questions, please open an issue in the GitHub repository.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndum%2Flivechat-backend","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fndum%2Flivechat-backend","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fndum%2Flivechat-backend/lists"}