{"id":26351373,"url":"https://github.com/swiichycode/plana-chatbot-microservice","last_synced_at":"2025-10-17T19:54:21.917Z","repository":{"id":278939644,"uuid":"937225090","full_name":"SwiichyCode/Plana-chatbot-microservice","owner":"SwiichyCode","description":"This microservice is designed to handle chatbot interactions efficiently by leveraging Redis for caching and PostgreSQL for persistent storage. The architecture ensures quick responses while maintaining a reliable conversation history.","archived":false,"fork":false,"pushed_at":"2025-02-22T17:28:47.000Z","size":26,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-22T17:32:46.706Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SwiichyCode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2025-02-22T16:25:45.000Z","updated_at":"2025-02-22T17:28:50.000Z","dependencies_parsed_at":"2025-02-22T17:43:02.062Z","dependency_job_id":null,"html_url":"https://github.com/SwiichyCode/Plana-chatbot-microservice","commit_stats":null,"previous_names":["swiichycode/plana-chatbot-microservice"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiichyCode%2FPlana-chatbot-microservice","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiichyCode%2FPlana-chatbot-microservice/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiichyCode%2FPlana-chatbot-microservice/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SwiichyCode%2FPlana-chatbot-microservice/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SwiichyCode","download_url":"https://codeload.github.com/SwiichyCode/Plana-chatbot-microservice/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243858794,"owners_count":20359257,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":"2025-03-16T10:31:34.851Z","updated_at":"2025-10-17T19:54:16.877Z","avatar_url":"https://github.com/SwiichyCode.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Chatbot Microservice with Redis \u0026 PostgreSQL\n\n## 📌 Overview\nThis microservice is designed to handle chatbot interactions efficiently by leveraging **Redis** for caching and **PostgreSQL** for persistent storage. The architecture ensures quick responses while maintaining a reliable conversation history.\n\n## 🏗️ Architecture\nThe chatbot service follows a **Producer-Consumer pattern** to achieve high performance and scalability:\n\n1. **Fast response**: Chat history is **temporarily stored in Redis** for quick retrieval.\n2. **Asynchronous persistence**: Each message is **queued in Redis** for background storage.\n3. **Worker process**: A background **worker listens to Redis** and saves conversations into **PostgreSQL**.\n\n### 📂 Project Structure\n```\n📂 chatbot-service/\n├── 📂 src/\n│   ├── 📂 config/       # Configuration (Redis, OpenAI, PostgreSQL)\n│   ├── 📂 controllers/  # Business logic for handling requests\n│   ├── 📂 services/     # Services (Chat handling, database operations)\n│   ├── 📂 workers/      # Background worker for PostgreSQL storage\n│   ├── 📂 models/       # Sequelize models for PostgreSQL\n│   ├── app.js          # Express app configuration\n│   ├── server.js       # Main server entry point\n│   ├── worker.js       # Worker process for persisting conversations\n├── .env\n├── package.json\n```\n\n## 🚀 Installation \u0026 Setup\n### 1️⃣ Clone the Repository\n```bash\ngit clone https://github.com/your-repo/chatbot-service.git\ncd chatbot-service\n```\n\n### 2️⃣ Install Dependencies\n```bash\nnpm install\n```\n\n### 3️⃣ Configure Environment Variables\nCreate a `.env` file with the following configuration:\n```env\nPORT=4000\nOPENAI_API_KEY=your_openai_api_key\nREDIS_HOST=localhost\nREDIS_PORT=6379\nPG_HOST=localhost\nPG_USER=your_pg_user\nPG_PASSWORD=your_pg_password\nPG_DATABASE=your_pg_database\nEXPIRATION_TIME=1800\n```\n\n### 4️⃣ Start the Services\nStart the **chatbot API**:\n```bash\nnpm run start\n```\nStart the **worker for database persistence**:\n```bash\nnpm run worker\n```\n\n## 🛠️ Implementation Details\n\n### 📌 Chatbot Service (`src/services/chatService.js`)\n- Stores conversation in **Redis** for quick access.\n- Enqueues messages in **Redis Queue** (`chatQueue`) for background storage.\n- Calls **OpenAI API** to generate responses.\n\n### 📌 PostgreSQL Storage Worker (`src/workers/worker.js`)\n- Listens to the `chatQueue` in Redis.\n- Saves the conversation history to **PostgreSQL** in the background.\n- Ensures persistent data storage **without affecting chatbot response time**.\n\n## 📈 Advantages of This Architecture\n✅ **High Performance**: Fast chatbot responses using **Redis cache**.\n✅ **Scalability**: Background worker allows **high throughput** for message storage.\n✅ **Resilience**: If PostgreSQL is down, **Redis retains messages** until it's back up.\n✅ **Reliability**: Messages are stored **permanently in PostgreSQL** for long-term history.\n\n## 🏗️ Future Enhancements\n- **Dockerization**: Deploy with Docker \u0026 Kubernetes.\n- **BullMQ Integration**: Replace Redis Queue with BullMQ for better job handling.\n- **Multi-user Sessions**: Handle multiple active conversations more efficiently.\n\n## 🎯 Conclusion\nThis chatbot microservice balances **performance, scalability, and reliability** by combining **Redis caching** with **PostgreSQL persistence**. The asynchronous **Worker pattern** ensures fast responses while keeping conversation history intact.\n\n🚀 Ready to deploy? Let’s get started! 🔥\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswiichycode%2Fplana-chatbot-microservice","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fswiichycode%2Fplana-chatbot-microservice","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fswiichycode%2Fplana-chatbot-microservice/lists"}