A robust Node.js backend API for the Twik chatting application, built with TypeScript, Express.js, and Socket.IO. This backend provides real-time messaging capabilities, user authentication, conversation management, and AI-powered daily questions using Google Gemini.
- Real-time Messaging: WebSocket-based chat using Socket.IO
- User Authentication: JWT-based authentication with bcrypt password hashing
- Conversation Management: Create and manage chat conversations
- Contact System: User contact management
- AI Integration: Daily conversation starters generated by Google Gemini AI
- Automated Tasks: Cron jobs for sending daily questions to all conversations
- PostgreSQL Database: Hosted on Neon for reliable data storage
- TypeScript: Full type safety and modern JavaScript features
- Runtime: Node.js
- Language: TypeScript
- Framework: Express.js
- Real-time: Socket.IO
- Database: PostgreSQL (Neon)
- Authentication: JWT (jsonwebtoken)
- Password Hashing: bcrypt
- AI Service: Google Generative AI (Gemini)
- Scheduling: node-cron
- Logging: Morgan
- Development: ts-node, nodemon
- Node.js (v18 or higher)
- npm or yarn
- PostgreSQL database (configured for Neon)
-
Clone the repository:
git clone https://github.com/Semicile17/twik-backend cd twik-backend -
Install dependencies:
npm install
-
Create a
.envfile in the root directory with the required environment variables (see Environment Variables section). -
Build the project:
npm run build
Create a .env file in the root directory with the following variables:
# Database Configuration
DB_USER=your_neon_db_user
DB_PASSWORD=your_neon_db_password
DB_HOST=your_neon_db_host
DB_PORT=5432
DB_NAME=your_db_name
DB_SSL=true
# Authentication
JWT_SECRET=your_jwt_secret_key
# AI Configuration
AI_BOT_ID=your_ai_bot_id
GEMINI_API_KEY=your_google_gemini_api_key
# Server
PORT=6000The application expects the following tables in your PostgreSQL database:
users- User accountsconversations- Chat conversationsmessages- Chat messagescontacts- User contacts
Ensure your database schema matches the application's expectations.
npm run devnpm run build
npm startThe server will start on the port specified in your .env file (default: 6000).
POST /auth/register- Register a new userPOST /auth/login- Login user
GET /conversations- Get user conversationsPOST /conversations- Create or check conversation
GET /messages/:conversationId- Get messages for a conversationPOST /messages- Send a message
GET /contacts- Get user contactsPOST /contacts- Add a contact
joinConversation- Join a conversation roomsendMessage- Send a message to a conversation
receiveMessage- Receive a new messageconversationUpdated- Conversation list update
The application includes an AI-powered feature that generates daily conversation starters:
- Uses Google Gemini AI to create fun, creative questions
- Automatically sends questions to all conversations at 9 AM daily via cron job
- Questions are kept under 20 words for optimal engagement
twik-backend/
├── src/
│ ├── controllers/ # Route handlers
│ ├── cron/ # Scheduled tasks
│ ├── middlewares/ # Express middlewares
│ ├── models/ # Database models
│ ├── routes/ # API routes
│ ├── services/ # External services (Gemini AI)
│ ├── types/ # TypeScript type definitions
│ ├── utils/ # Utility functions
│ ├── config.ts # Configuration
│ └── index.ts # Application entry point
├── dist/ # Compiled JavaScript
├── .env # Environment variables
├── package.json
├── tsconfig.json
├── nodemon.json
└── README.md
npm run dev- Start development server with hot reloadnpm run build- Compile TypeScript to JavaScriptnpm start- Start production server
The application uses:
- TypeScript for type safety
- ts-node for running TypeScript directly
- nodemon for automatic restarts during development
- Morgan for HTTP request logging
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
ISC License
For support or questions, please open an issue in the repository.