Hi sir, here I will show my app chat project.
- Login API
url : localhost:8080/api/auth/login (POST)
body : {"phone" : "...","password" : "..."}
- Register API
url : localhost:8080/api/auth/register (POST)
body : {"phone" : "...","name" : "...","password" : "..."}
- Get Contact API
url : localhost:8080/api/contact (GET)
header : {Authorization : "Bearer ..."}
- Get Chat
url : localhost:8080/api/chat/:contactId/:lastId (GET)
header : {Authorization : "Bearer ..."}
note : This API use pagination. When load first page, set lastId to "nil". For another page, set lastId according to last id chat that get before.
- New Chat
url : localhost:8080/api/new_chat (POST)
header : {Authorization : "Bearer ..."}
body : {"phone": "...", "message": "..."}
- Websocket
url : localhost:8080/ws/:user_id
header : {Authorization : “Bearer …”}
-
Register, Login, & Logout
-
Add other user to their contacts
-
Realtime chat with other user
-
See chat history
-
Gin for routing (github.com/gin-gonic/gin)
-
JWT Auth for authentication using token (github.com/dgrijalva/jwt-go)
-
Validator for validate request (github.com/go-playground/validator/v10)
-
Gorilla Websocket for communicate using websocket (github.com/gorilla/websocket)
-
Godotenv for environment setting (github.com/joho/godotenv)
-
Mongo Driver for connect to mongoDB (go.mongodb.org/mongo-driver)
- Register page
- Login page
- Home page
- Send new message
- Incoming message
- Continue chatting
-
Make sure you have installed golang (https://golang.org/doc/install) & mongoDB (https://docs.mongodb.com/manual/installation/)
-
Clone this repository to your PC
-
Go to project directory and open setup_database.js file
-
Copy all script in that file
-
Now, open MongoDB terminal
5.a. Open terminal (Because I use windows, I open command prompt)
5.b. Go to mongo directory (my mongo dir is : "C:\Program Files\MongoDB\Server\4.4\bin"
5.c. Then type "mongo"
- Run all script to that mongo terminal (just paste it)
-
Wait until done.
-
Open new terminal and go to project directory
-
Run the following command :
go build
go run .
-
Open 2 different browser and go to localhost:8080/register to test it
-
Register with name, phone, and password
-
After register, login using that account
-
Try to send new message
-
Open project directory
-
Download and install postman
-
Open postman
-
Create new workspace
-
Import postman file from PROJECT_DIRECTORY/requirement/postman/API.postman_collection.json
note : postman is only used to run restful API (because until now postman still doesn't support websocket)
I have do some optimization for server & database performance :
-
Create a fast performing database structure in MongoDB
I've tried several possible database structures, and the last structure I tried was fast enough. I use 3 collection : users, contacts, and chats. It faster than I just use 2 collection : users, contacts (Chat data is in the contacts collection as array) -
Add indexes in certain collections as needed
I have added indexes to 3 collections in certain fields so that the reading process is faster. -
Using go routine so that the websocket process can run simultaneously
I use go routine to listen and write data to client. I also use channel as a "bridge" for websocket data communications, like example when user connect to websocket, send message, retreive message. -
Using mutex to prevent deadlock
I use mutex when process writing to websocket, because it can prevent deadlock. -
Make queries as efficient as possible
I test every query on mongo terminal and measure that time execution (using profile mode and explain() function). I also try to make 100.000 user with 5.000 contact for each user and 10.000 chat for 300 first contact. I try all query that needed in backend, and it still fine (after I adding index). I also follow some suggestion about optimization query from mongo documentation (ex : https://docs.mongodb.com/manual/reference/operator/aggregation/match/#pipeline-optimization)
For the future, if this application develops with more complex business processes and our servers are too busy, maybe we can do several things such as adding new servers, implementing microservices, sharding in Mongodb, etc.