Skip to content

Devanshu11976/Chat-System

Repository files navigation

Typing SVG

Java Node.js WebSocket AI Powered Maven


A production-grade, real-time multi-client chat system built from scratch using raw Java Sockets β€” no frameworks, no shortcuts. Powered by Groq AI for toxicity detection and smart replies.


GitHub repo Status License


πŸ‘¨β€πŸ’» Authors

Devanshu Sharma Sanmati Jain

✨ Features

Feature Description
πŸ”Œ Raw TCP Chat Multi-client real-time chat using Java ServerSocket β€” zero frameworks
🧡 Thread per Client Dedicated ClientHandler thread for each connected user
🏠 Multiple Rooms Create, join, and leave chat rooms dynamically
πŸ€– AI Toxicity Detection Groq AI (Llama 3.3 70B) scans every message before broadcast
πŸ’‘ Smart Replies /ai command generates 3 context-aware reply suggestions
🌐 Browser Support WebSocket β†’ TCP proxy bridges browser clients to Java server
πŸ“‘ REST API Full HTTP API for rooms, history, stats
πŸ’¬ Private Messaging Direct messages between users via /pm command

πŸ—οΈ Architecture

Browser Clients
      β”‚  WebSocket (ws://localhost:3001)
      β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚  proxy.js   β”‚  Node.js WebSocket β†’ TCP Bridge
 β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
        β”‚  TCP (port 9291)
        β–Ό
 β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
 β”‚         Java Chat Server             β”‚
 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
 β”‚  β”‚ ChatServer  β”‚  β”‚  RestApiServerβ”‚ β”‚
 β”‚  β”‚  port 9291  β”‚  β”‚   port 8281   β”‚ β”‚
 β”‚  β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
 β”‚         β”‚                           β”‚
 β”‚  β”Œβ”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
 β”‚  β”‚ClientHandlerβ”‚  β”‚  AIService    β”‚ β”‚
 β”‚  β”‚(per thread) β”‚  β”‚  Groq API     β”‚ β”‚
 β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
 β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸ“ Project Structure

chat-system/
β”œβ”€β”€ πŸ“„ pom.xml
β”œβ”€β”€ πŸ“„ README.md
β”œβ”€β”€ πŸ“‚ frontend/
β”‚   β”œβ”€β”€ 🌐 index.html          ← Single page UI
β”‚   β”œβ”€β”€ ⚑ proxy.js             ← WebSocket β†’ TCP bridge
β”‚   └── πŸ” restart_servers.ps1
β”œβ”€β”€ πŸ“‚ src/main/java/com/chat/
β”‚   β”œβ”€β”€ πŸš€ Main.java
β”‚   β”œβ”€β”€ πŸ“‚ chat/
β”‚   β”‚   β”œβ”€β”€ ChatServer.java     ← ServerSocket server
β”‚   β”‚   β”œβ”€β”€ ClientHandler.java  ← Thread per client
β”‚   β”‚   └── CommandProcessor.java
β”‚   β”œβ”€β”€ πŸ“‚ server/
β”‚   β”‚   └── RestApiServer.java  ← HTTP API (port 8281)
β”‚   β”œβ”€β”€ πŸ“‚ services/
β”‚   β”‚   β”œβ”€β”€ MessageService.java
β”‚   β”‚   └── AIService.java      ← Groq AI integration
β”‚   β”œβ”€β”€ πŸ“‚ models/
β”‚   β”œβ”€β”€ πŸ“‚ exceptions/
β”‚   └── πŸ“‚ utils/
β”‚       └── ApiConfig.java
└── πŸ§ͺ tcp_test.js

πŸš€ Quick Start

Prerequisites

  • Java 17+
  • Maven 3.8+
  • Node.js 18+
  • Free Groq API key β†’ console.groq.com (no card needed)

1. Clone the repo

git clone https://github.com/Devanshu11976/Chat-System.git
cd Chat-System/chat-system

2. Set your Groq API key

# PowerShell
$env:GROQ_API_KEY="your_groq_key_here"
# Mac / Linux
export GROQ_API_KEY="your_groq_key_here"

3. Build & run the Java server

# Build
mvn package

# Run (PowerShell)
$env:CHAT_SOCKET_PORT='9291'
$env:CHAT_REST_PORT='8281'
java -cp "target\classes;lib\json-20231013.jar" com.chat.Main

4. Start the WebSocket proxy

# Open a new terminal
cd frontend
$env:WS_PORT='3001'
$env:CHAT_PORT='9291'
node proxy.js

5. Open the frontend

Open frontend/index.html in your browser

🌐 REST API Endpoints

Method Endpoint Description
GET /api/rooms List all chat rooms
GET /api/rooms/{roomId} Room details + members
GET /api/rooms/{roomId}/history Message history
GET /api/users Online users
GET /api/stats Server statistics
GET /api/moderation/log AI toxicity log

Example

curl http://localhost:8281/api/rooms
curl http://localhost:8281/api/stats

πŸ’¬ Chat Commands

Command Description
/join <room> Join or create a room
/leave Leave current room
/msg <text> Send message to room
/pm <username> <text> Private message
/ai <context> Get 3 AI smart reply suggestions
/users List users in current room
/rooms List all active rooms
/quit Disconnect

πŸ€– AI Features

Toxicity Detection

Every message is analyzed by Groq AI (Llama 3.3 70B) before broadcast:

Score Action Display
0 – 30 βœ… Allow Normal message
31 – 60 ⚠️ Warn Flagged with warning tag
61 – 100 🚫 Block Hidden + sender notified

3 blocked messages β†’ automatic disconnect

Smart Replies (/ai)

Generates 3 context-aware reply suggestions with tone labels: friendly Β· professional Β· casual Β· funny


πŸ”Œ Ports Reference

Service Default Port Env Variable
Java TCP Chat Server 9291 CHAT_SOCKET_PORT
Java REST API 8281 CHAT_REST_PORT
Node WebSocket Proxy 3001 WS_PORT

πŸ§ͺ Testing

# Run the automated TCP test
node tcp_test.js

This script connects via TCP, sets a username, creates the autotest room, sends a message, and verifies server responses automatically.


πŸ› Troubleshooting

Port already in use

# Find and kill the process
netstat -ano | findstr :9291
taskkill /PID <pid> /F

Frontend not connecting

  • Make sure proxy.js is running on port 3001
  • Check WS_URL constant in frontend/index.html matches proxy port
  • Verify REST API is up: curl http://localhost:8281/api/rooms

"Unknown command" in chat

  • Check proxy.log β€” proxy forwards raw text to TCP
  • Ensure command format is exact, e.g. /join general not /join/general

AI not responding

  • Verify GROQ_API_KEY env variable is set
  • App has automatic fallback to keyword-based moderation if Groq is unavailable

πŸ› οΈ Tech Stack

Layer Technology
Chat Server Java 17 Β· ServerSocket Β· ConcurrentHashMap
Threading Thread per client Β· AtomicInteger Β· synchronized
REST API com.sun.net.httpserver.HttpServer
JSON org.json
WS Bridge Node.js Β· ws library
AI Groq API Β· Llama 3.3 70B
Build Maven

πŸ“œ License

MIT Β© 2026 Devanshu Sharma & Sanmati Jain


Built with ❀️ using raw Core Java β€” no frameworks, no shortcuts.

"If you can't explain it without a framework, you don't understand it."


πŸ’œ Made with love by Dev & San

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors