Skip to content

Semicile17/twik-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Twik Backend

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.

Features

  • 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

Tech Stack

  • 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

Prerequisites

  • Node.js (v18 or higher)
  • npm or yarn
  • PostgreSQL database (configured for Neon)

Installation

  1. Clone the repository:

    git clone https://github.com/Semicile17/twik-backend
    cd twik-backend
  2. Install dependencies:

    npm install
  3. Create a .env file in the root directory with the required environment variables (see Environment Variables section).

  4. Build the project:

    npm run build

Environment Variables

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=6000

Database Setup

The application expects the following tables in your PostgreSQL database:

  • users - User accounts
  • conversations - Chat conversations
  • messages - Chat messages
  • contacts - User contacts

Ensure your database schema matches the application's expectations.

Running the Application

Development Mode

npm run dev

Production Mode

npm run build
npm start

The server will start on the port specified in your .env file (default: 6000).

API Endpoints

Authentication

  • POST /auth/register - Register a new user
  • POST /auth/login - Login user

Conversations

  • GET /conversations - Get user conversations
  • POST /conversations - Create or check conversation

Messages

  • GET /messages/:conversationId - Get messages for a conversation
  • POST /messages - Send a message

Contacts

  • GET /contacts - Get user contacts
  • POST /contacts - Add a contact

Real-time Events (Socket.IO)

Client Events

  • joinConversation - Join a conversation room
  • sendMessage - Send a message to a conversation

Server Events

  • receiveMessage - Receive a new message
  • conversationUpdated - Conversation list update

AI Features

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

Project Structure

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

Scripts

  • npm run dev - Start development server with hot reload
  • npm run build - Compile TypeScript to JavaScript
  • npm start - Start production server

Development

The application uses:

  • TypeScript for type safety
  • ts-node for running TypeScript directly
  • nodemon for automatic restarts during development
  • Morgan for HTTP request logging

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

License

ISC License

Support

For support or questions, please open an issue in the repository.