[TIL] Real-time Chat API
05/13/23
![[TIL] Real-time Chat API](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1688362148483%2F084a482b-d8f0-44cf-8d38-5bd343eda36e.png&w=3840&q=75)
๐ Issues encountered
Issue #1
Sending a pull request to the develop branch instead of the main branch of the original repository.
- We decided to use the develop branch and feature branches in the original repository based on the git flow strategy.
Issue #2
How to store real-time chat records implemented using sockets in a database?
- We faced the challenge of determining the best approach to store chat records in a database while using sockets for real-time communication.
๐ What I tried
Issue #1
We decided to manage branches by adding role-specific names after the feature/ prefix, such as feature/chats.
First, we synchronized the forked repository with the original repository to create the develop branch in the forked repository. Therefore, the forked repository had three branches: main, develop, and feature/chats.
All the code related to the assigned task was managed in the feature/chats branch.
Before sending a pull request from the feature/chats branch to the develop branch of the original repository, we ensured that the develop branch in the forked repository was synchronized. Without syncing, an "cannot merge" error would occur in the pull request.
Issue #2
For storing real-time chat records in the database, we decided to send periodic PATCH requests from the frontend.
We used
setInterval()to send PATCH requests at regular intervals. We stored the last index of the already stored chat records in the database and created an array of the unsaved chat records. This array was then sent to the server as the request body.The server received this array from the request body, processed it by using
stringify, and added the updated chat records to the content column.In small-scale projects like ours, sending frequent INSERT requests to an RDBMS is generally manageable. However, for large-scale real-time chat services, it is recommended to consider using a JSON-based NoSQL database or a serverless real-time database like Firebase, where chat records can be directly sent from the frontend to the database and retrieved.
If time allows and the socket implementation with the frontend is successful, we plan to explore storing chat records in a database like MongoDB.
![[์ฝํ
] ๊ทธ๋ฆฌ๋ ๋ฌธ์ - ๋ฌด์ง์ ๋จน๋ฐฉ ๋ผ์ด๋ธ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1712215455263%2F1ac1f35a-8862-4e42-8d0c-e2bea01e04c0.png&w=3840&q=75)
![[์ฝํ
] Bfs ํ ๋งํ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709032619170%2F70056896-c857-444b-9c99-45bfcb466806.png&w=3840&q=75)
![[์ฝํ
] Dfs ๋ฌธ์ ์ ํ - ๊ทธ๋ํ ๋ด์์ ๊ตฌ๋ถํ์ฌ ์นด์ดํธ ํ๊ธฐ](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1709019361383%2Fb0585d72-c808-4169-83a9-2724f312e927.png&w=3840&q=75)
![[์ฝํ
] DFS vs BFS](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971211123%2F71f9386c-6a62-43b2-a602-4d084c24d6cf.png&w=3840&q=75)
![[์ฝํ
] ์ฌํ๊ฒฝ๋ก](/_next/image?url=https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1708971251412%2F27ce72ed-8ee7-4d13-a02f-ff4bbe50c4be.png&w=3840&q=75)