8000 GitHub - ilumin/chat-kong-socket.io
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

ilumin/chat-kong-socket.io

Repository files navigation

Chat Application with Kong Gateway and Socket.io

A simple real-time chat application built with Socket.io, running behind Kong API Gateway, and containerized with Docker. Features persistent message storage and user session management.

Features

  • Real-time messaging using Socket.io
  • User join/leave notifications
  • List of currently online users
  • API Gateway integration with Kong
  • Fully dockerized setup
  • Persistent chat messages using MongoDB
  • User session management with Redis
  • Automatic session restoration when users reconnect

Architecture

This project consists of:

  1. Chat Application: Node.js application using Express and Socket.io
  2. Kong Gateway: API Gateway that routes traffic to the chat application
  3. MongoDB: Database for storing chat messages and user information
  4. Redis: For session management and Socket.io scaling

Prerequisites

  • Docker and Docker Compose
  • Git

Getting Started

Clone the Repository

git clone <your-repository-url>
cd chat-kong-socketio

Running the Application

Start the application with Docker Compose:

docker-compose up --build

This will:

  • Build the chat application image
  • Set up the Kong database
  • Run Kong migrations
  • Start the Kong Gateway
  • Configure Kong routes and services

Accessing the Application

The chat application can be accessed in two ways:

Using the Chat Application

  1. Enter a username to join the chat
  2. Start sending messages
  3. See real-time updates as users join and leave
  4. View the list of currently online users

Project Structure

├── docker-compose.yml    # Docker compose configuration
├── Dockerfile            # Chat application Docker image
├── package.json          # Node.js dependencies
├── server.js             # Socket.io server implementation
├── .env                  # Environment variables
├── models/               # MongoDB model definitions
│   ├── Message.js        # Chat message schema
│   └── User.js           # User schema
├── kong-setup/           # Kong Gateway configuration
│   ├── Dockerfile        # Kong setup container
│   └── setup.sh          # Kong configuration script
└── public/               # Frontend assets
    ├── index.html        # Chat UI
    ├── main.js           # Frontend JavaScript
    └── styles.css        # CSS styles

Persistence Features

Message Persistence

All chat messages are stored in MongoDB, allowing users to see previous conversations when they join the chat. The system loads the most recent 50 messages when a user connects.

User Sessions

User sessions are managed with Redis, allowing users to:

  • Maintain their identity across page refreshes
  • Automatically reconnect with their username when returning to the chat
  • Keep track of user activity for better user management

Kong Gateway Configuration

The Kong Gateway is configured to:

  • Route traffic from /chat path to the chat application
  • Enable CORS for WebSocket support
  • Allow real-time Socket.io connections through the gateway

The Kong Admin API is available at http://localhost:8001.

Development

Modifying the Frontend

The frontend code is located in the public directory. You can modify:

  • index.html for layout changes
  • styles.css for styling
  • main.js for frontend behavior

Modifying the Backend

The chat server logic is in server.js. You can enhance it with:

  • Authentication
  • Message persistence
  • Multiple chat rooms
  • Private messaging

Customizing Kong

To modify Kong Gateway settings, edit the kong-setup/setup.sh script and rebuild the containers:

docker-compose down
docker-compose up --build

Working with the Database

You can access the MongoDB directly using standard MongoDB tools:

docker exec -it mongodb mongosh mongodb://root:rootpassword@localhost:27017/chatapp

To inspect Redis data:

docker exec -it redis redis-cli

Scaling

This application is designed to be scalable:

  • Redis adapter for Socket.io allows running multiple chat server instances
  • Session sharing via Redis enables load balancing across instances
  • MongoDB provides persistent storage for messages

Stopping the Application

To stop and remove containers:

docker-compose down

To completely reset everything including volumes:

docker-compose down -v

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0