Chat API with support for multiple users, multiple rooms, and real-time WebSocket communication.
.
├── cmd/server/main.go # Entry point
├── internal/ # Application core
│ ├── db/ # Database connection
│ ├── handler/ # HTTP handlers
│ ├── model/ # Data models
│ ├── repository/ # Database access layer
│ ├── router/ # HTTP routes setup
│ └── service/ # Business logic layer
| Method | Route | Description |
|---|---|---|
| GET | /user |
List all users |
| GET | /user/:id |
Get user by ID |
| POST | /user |
Create new user |
| PUT | /user/:id |
Update user |
| DELETE | /user/:id |
Delete user |
| GET | /user/username/:username |
Find user by username |
| GET | /user/chat/:chat_id |
Find users by chat ID |
| Method | Route | Description |
|---|---|---|
| GET | /chat |
List all chats |
| GET | /chat/:id |
Get chat by ID |
| POST | /chat |
Create new chat |
| PUT | /chat/:id |
Update chat |
| DELETE | /chat/:id |
Delete chat |
| GET | /chat/user/:user_id |
Add user to chat |
| DELETE | /chat/user/:user_id |
Remove user from chat |
| GET | /chat/user/:user_id/chat/:chat_id/access |
Check if user has access to a chat |
| Method | Route | Description |
|---|---|---|
| GET | /ws |
WebSocket connection endpoint |
{
"type": "connect-chat",
"content": {
"customerd": 1,
"chatId": 1,
"username": "name"
}
}{
"type": "send-message",
"content": {
"chatId": 1,
"customerId": 1,
"content": "Hi world."
}
}{
"type": "update-message",
"content": {
"chatId": 1,
"customerId": 1,
"content": "Hello world!"
}
}{
"type": "delete-message",
"content": 10
}-
Each chat has its own
Hub. -
clientsis a map of active WebSocket connections per chat. -
Messages are processed based on their
type:joinandleavemessages are automatically broadcasted.send-message,update-message, anddelete-messageare broadcasted and persisted.
-
Connection starts with a
connect-chatmessage.
- Client sends a message with
"type": "connect-chat". - Server creates or retrieves the
Hubfor the chat. - Client is registered in the
Hub. - Server listens to incoming messages and forwards them to all clients in the same chat.
- Create a
.envfile with necessary environment variables. - Run the SQL script to create and populate the database tables.
- (Optional) Import the Postman collection for testing.
- Start the application:
go run cmd/server/main.go