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.
- 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
This project consists of:
- Chat Application: Node.js application using Express and Socket.io
- Kong Gateway: API Gateway that routes traffic to the chat application
- MongoDB: Database for storing chat messages and user information
- Redis: For session management and Socket.io scaling
- Docker and Docker Compose
- Git
git clone <your-repository-url>
cd chat-kong-socketio
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
The chat application can be accessed in two ways:
- Direct access: http://localhost:3000
- Through Kong Gateway: http://localhost:8000/chat
- Enter a username to join the chat
- Start sending messages
- See real-time updates as users join and leave
- View the list of currently online users
├── 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
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 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
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.
The frontend code is located in the public
directory. You can modify:
index.html
for layout changesstyles.css
for stylingmain.js
for frontend behavior
The chat server logic is in server.js
. You can enhance it with:
- Authentication
- Message persistence
- Multiple chat rooms
- Private messaging
To modify Kong Gateway settings, edit the kong-setup/setup.sh
script and rebuild the containers:
docker-compose down
docker-compose up --build
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
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
To stop and remove containers:
docker-compose down
To completely reset everything including volumes:
docker-compose down -v