Backend Application for Chat with Firebase
Simple chat application made with Firebase and Go Fiber.
- Add these environment variables to
.env
file.
SERVER_HOST=<PORT>
SERVER_PORT=<HOST>
FIREBASE_CREDS=<JSON_CREDS_PATH>
FIREBASE_DB_URI=<FIRESTORE_PROJECT_ID>
- Use
go run .
orgo build
to install all dependencies and run the application.
- The user will initialize a chat first. The application tracks all participants in the chatroom. This way, aside from one-to-one or personal communication, a one-to-all or group chat communication can be done.
- If a user send a new message, the system will track the chat in which the user send the message in. This way, the recepients of the chat can be correctly found by the system.
- The system can then list out all chats a user has, based on the list of the participants in every chats, along with the messages in those chats.
First time using Firebase
! Decided to use Firestore
on top of Realtime Database
because it has the ability to structure the database into collections. I use Firestore ability to store a subcollection
(collection inside collection). In this case, each chat will have a list of messages.
root
= /api/v1
POST /chats
{
"participants_id": [<user_id1>, <user_id1>, ...]
}
Returns chat_id
for the newly created chat.
POST /chats/message
{
"chat_id": <chat_id>,
"sender_id": <sender_id>,
"sent_at": <iso_timestamp>,
"content": <content_text>
}
Returns message_id
for the newly created message.
GET /chats/:user_id
Returns a list of chats with corresponding messages in each chat.
- Better and more scalable models design(?)
- A bit more on the clarity of response returned by the endpoints.