Skip to main content

Chat

Chat Data Model and APIs

Diagram example

Database Tables

User table

NameTypeField
idINTPRIMARY KEY
usernameVARCHARNOT NULL
passwordVARCHARNOT NULL
emailVARCHARNOT NULL
phoneVARCHARNOT NULL

Conversation table

NameTypeField
idINTPRIMARY KEY
user_idINTNOT NULL
nameVARCHARNOT NULL
created_atTIMESTAMPNOT NULL

Message table

NameTypeField
idINTPRIMARY KEY
conversation_idINTNOT NULL
user_idINTNOT NULL
messageTEXTNOT NULL
created_atTIMESTAMPNOT NULL

Participant table

NameTypeField
idINTPRIMARY KEY
conversation_idINTNOT NULL
user_idINTNOT NULL

Group table

NameTypeField
idINTPRIMARY KEY
nameVARCHARNOT NULL
created_atTIMESTAMPNOT NULL

Group Participants table

NameTypeField
idINTPRIMARY KEY
group_idINTNOT NULL
user_idINTNOT NULL

APIs

APIs and Microservices

  • Authentication and Authorization API: This service would handle user registration, login, and authentication. It would also handle user roles and permissions.

  • User Profile API: This service would handle the management of user profiles, including personal information, contact information, and status updates.

  • Chat API: This service would handle the management of chat functionality, including creating and managing conversations, sending and receiving messages, and handling media files.

    • Conversation Microservice: This service would handle the management of conversations, including creating new conversations, retrieving conversation information, and handling the deletion of conversations.

    • Message Microservice: This service would handle the management of messages, including sending and receiving messages, editing and deleting messages, and handling message history.

    • Media Microservice: This service would handle the management of media files, including uploading, downloading, and handling file types.

    • Group Microservice: This service would handle the management of groups, including creating new groups, retrieving group information, adding and removing group participants, handling group invitations and managing group settings.

    • Search Microservice: This service would handle search functionality, including searching for users, conversations, and messages.

  • Group API: This service would handle the management of groups, including creating and managing groups, adding and removing participants, and handling group invitations.

  • Blocking API: This service would handle the management of blocked users, including adding and removing blocked users.

  • Push Notification API: This service would handle the sending of push notifications to users, including updates on new messages and status changes.

  • Data Analysis API: This service would handle the collection and analysis of data from the app to inform business decisions and improve the overall user experience.

  • Device Management API: This service would handle the management of a user's devices, including device registration and push notification handling.

API-DB Connection

  • A user opens the WhatsApp-like chat app on their device and logs in.

  • The app sends an authentication request to the Authentication and Authorization API, which verifies the user's credentials and generates an access token.

  • The user wants to create a new conversation, the app sends a request to the Chat API, including the conversation details and the access token.

  • The Chat API, in turn, sends a message containing the request for creating a new conversation to the corresponding microservice (e.g. Conversation Microservice) using Kafka.

  • The Conversation Microservice receives the message, processes it, and creates a new conversation in the database.

  • The user wants to send a message, the app sends a request to the Chat API, including the message details and the access token.

  • The Chat API, in turn, sends a message containing the request for sending the message to the corresponding microservice (e.g. Message Microservice) using Kafka.

  • The Message Microservice receives the message, processes it, stores the message in the database and sends a message to all participants of the conversation with the new message.

  • The Chat API receives the message and sends a push notification to the recipients.

  • The app receives the message and shows it to the user.

These tables can be used for storing user information, conversation information, message information, and group information. The relationships between the tables are:

  • A user can have many conversations and many messages.
  • A conversation can have many participants and many messages.
  • A group can have many participants.
  • It should be noted that this is a simple representation of the database design for a chat app, and additional tables or fields may be needed for more complex functionality, such as handling media files, or storing user status.

Media table

NameTypeField
idINTPRIMARY KEY
message_idINTNOT NULL
typeVARCHARNOT NULL
urlVARCHARNOT NULL

Status table

NameTypeField
idINTPRIMARY KEY
user_idINTNOT NULL
statusVARCHARNOT NULL
created_atTIMESTAMPNOT NULL

Blocked table

NameTypeField
idINTPRIMARY KEY
user_idINTNOT NULL
blocked_user_idINTNOT NULL

Group Invitation table

NameTypeField
idINTPRIMARY KEY
group_idINTNOT NULL
invited_user_idINTNOT NULL
statusVARCHARNOT NULL

Device table

NameTypeField
idINTPRIMARY KEY
user_idINTNOT NULL
device_idVARCHARNOT NULL
device_typeVARCHARNOT NULL

The media table is used to store information about media files like images, videos, etc. that are shared in the chat. The status table is used to store a user's status updates. The blocked table is used to store information about blocked users. The group invitation table is used to store information about group invitations sent to users and their status. The device table is used to store information about a user's devices, such as device ID and device type.