Database Design for Chat Application

Поделиться
HTML-код
  • Опубликовано: 4 окт 2024

Комментарии • 77

  • @asim-gandu-phenchod
    @asim-gandu-phenchod Год назад +5

    The whole video was special. You are a TOP G. Keep it up bro

  • @codexperience7951
    @codexperience7951 Год назад +2

    nice erd for chat systems, will use yours and enhance it base to what i needed. thanks to your video

  • @NadjibSamsung
    @NadjibSamsung 5 месяцев назад +1

    I think this design is vulnerable. Because any authenticated user can alter the value of the sender id. The solution is to verify that the value of the sender id is the same value of the user id stored in the sessions store.

    • @DatabaseStar
      @DatabaseStar  5 месяцев назад +1

      Good point. This is just the database and there would be more involved in the application on authentication and ensuring people can only do the things they need to do. There would be no way for a user to edit IDs in a table.

  • @Иван-д6ф6с
    @Иван-д6ф6с 13 дней назад

    Hi, could you please extend your tutorial to include case how do you go about handling "media" attachments, "last seen", "read by" please.

  • @gabrielcosta1159
    @gabrielcosta1159 2 года назад +8

    Hey man, nice video! I'm new to this database thing, so I got a question that might be a bit stupid hahaha. If I want to send a message to only one contact, how would that work since the table contact is only linked directly to the table "group_member"? I mean, I would have to use the "group_member" table even when the message is not sent to a group, right? I'm kinda confused :x

    • @DatabaseStar
      @DatabaseStar  2 года назад +5

      Hi Gabriel, good question! Yes I think it would use the same concept, even if it is just to one contact. The group would have just the one contact.

    • @gabrielcosta1159
      @gabrielcosta1159 2 года назад

      @@DatabaseStar Oh ok.. Thank you so much!

    • @shankhashubhradas1586
      @shankhashubhradas1586 Год назад +1

      I also got the same doubt. Thanks @gabrielcosta1159 for asking and @DatabaseStar for answering 👍

  • @ymahtab
    @ymahtab 7 месяцев назад +1

    Hi, great video as always. One question: in the message table, why do we need to distinguish from_number and to_number from contacts? We could have from_contact_id and to_contact_id as FKs to the contact table. I feel like one table you're missing is a user's contact list (probably need a joining table) which would let the application figure out if the message was sent to a phone number or a saved contact. Does this make sense?
    EDIT: ok I think you covered this at around 4:30. We want to distinguish the contact's number from the recipient number the message was sent to at the time.

    • @DatabaseStar
      @DatabaseStar  7 месяцев назад

      Good question, yeah that's right!

  • @idkanymoreman
    @idkanymoreman 2 года назад +3

    awesome stuff.. thanks for the clear explanation..
    one question.. as the table grows, wont it be slower to query and fetch the chat messages if we store them in one table?

    • @DatabaseStar
      @DatabaseStar  2 года назад +3

      Good question. It would get slower eventually, but databases are very good at handling large amounts of data. A table with a million rows is quite manageable, for example. You can also add indexes, and other performance improvements, to help your queries.

  • @sidforreal
    @sidforreal Год назад +1

    Why contact and group_member is not a many to many relationship?
    One group can have multiple contacts, and one contact can be present in multiple groups

    • @DatabaseStar
      @DatabaseStar  Год назад

      That's a good point - it should be many-to-many actually. And therefore it would need a joining table between the two. Thanks for pointing it out.

  • @iremozen7172
    @iremozen7172 2 года назад +2

    that is a great video!! thank you. could you make a video for a simple delivery application?

    • @DatabaseStar
      @DatabaseStar  2 года назад +1

      Thanks! What do you mean by a “simple delivery application”? Like a food delivery service or something else?

    • @iremozen7172
      @iremozen7172 2 года назад +1

      @@DatabaseStar exactly, food delivery app would be awesome

  • @johnnychin9598
    @johnnychin9598 13 дней назад

    Sir, do you have a video were in the product item have different price depending on the customer. Say

    • @DatabaseStar
      @DatabaseStar  12 дней назад

      I don't have a video for that, but you could enhance an existing database to do this.

  • @BcomingHIM
    @BcomingHIM 2 месяца назад

    I dont understand this towards the end...how can contact and group member have one to many relationship?

    • @DatabaseStar
      @DatabaseStar  Месяц назад

      Good question. It’s because a contact can be in multiple groups in a chat application. A group for family and a group for their sports club friends and a group for other friends, for example.

  • @acbusiness2023
    @acbusiness2023 Год назад

    Really impressed... love this stuff...

  • @eugenefedorov3498
    @eugenefedorov3498 Год назад

    But it will be a crazy query to get only 20 last msgs for specific conversations id, as it will need to filter a table from millions and millions other msgs. I dont know the right way right now, only know few with some pros and cons. Save all msgs as a json in field. But then it will be really hard to write new msgs each time. Other idea is to create for each conversations new table. But then it means we will have millions of tables in data base. But still i think this is the best aproach.

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      That's good you're considering other options. The query itself won't be too crazy. It would be something like this (in MySQL):
      SELECT message_id, from_number, message_text, sent_datetime
      FROM message
      WHERE conversation_id = X
      ORDER BY sent_datetime DESC
      LIMIT 20;
      The database shouldn't have an issue looking through millions of messages if you have an index on the conversation_id column. If it does, you can then look into other performance strategies.

  • @jatinsutaria7992
    @jatinsutaria7992 Год назад +1

    Good stuff. However, how would you deal with figuring online users who need to be send messages to? Also, offline users, when they get online - how would this design help dispatch the missed messages to these users?

    • @DatabaseStar
      @DatabaseStar  Год назад +2

      I think regardless of whether they were online or offline the message would still be stored in the system. Perhaps there could be some code in the application to display all messages since the user last logged in and show them as new messages. I don’t know how you would determine if a user is online or offline though.

  • @john-zu3uo
    @john-zu3uo 2 года назад

    Thanks for this video sir!!

  • @hanatw-gj5ei
    @hanatw-gj5ei Год назад +2

    شكراا

  • @juhairahamed5342
    @juhairahamed5342 2 года назад

    Good explanation

  • @mohammadrezarajabali7591
    @mohammadrezarajabali7591 4 месяца назад

    Thanks...

  • @BaoNguyen-yg3vp
    @BaoNguyen-yg3vp 2 года назад

    With messages being stored persistently and the above relational data model. Which database is most suitable for chat app?

    • @DatabaseStar
      @DatabaseStar  2 года назад

      I’m not sure. You could get started with any sql database and it should work, I think.

  • @thebulletkin8393
    @thebulletkin8393 Год назад

    In the group_member table, I thought every record would need to have a primary key in order to be uniquely identifiable, but that table only has two foreign keys. Do those keys also serve the same purpose as a primary key then?

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      Good point! Yes the combination of both columns should be unique. This could be done as a primary key (both columns would be included in the primary key) or as a unique constraint.

  • @rockon7478
    @rockon7478 2 года назад +2

    I am wondering if this database design was for the on-device database like of watsapp or for the central hosted database ??

    • @DatabaseStar
      @DatabaseStar  2 года назад

      The idea was that it would be the central hosted database, but the on-device database could be different.

  • @practicalbong1497
    @practicalbong1497 Год назад

    if we want single user to send a message and or send to to a group how will i do that here ?

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      To do that, you would add a record to the message table with the details of the message, with a link to the conversation. This conversation could have one or many people in it.

  • @yenegew-tech
    @yenegew-tech 2 года назад

    Great

  • @palspal2329
    @palspal2329 10 месяцев назад

    Are u sure a sql db would be fine for this usecase?

    • @DatabaseStar
      @DatabaseStar  10 месяцев назад

      Yes it should be fine to use for this kind of design.

  • @Asurow
    @Asurow Год назад

    what if Message table have message_id, message_text, from_contact_id , to_contact_id, send_datetime and Group_member_Id fk . Group_member table have Group_member_Id , message_id fk and Contact_id fk.

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      Yeah, that could work, but I think it would only allow a message to be sent to a single contact.

  • @falseloop
    @falseloop Год назад

    What about temporary like 24hr story system

    • @DatabaseStar
      @DatabaseStar  11 месяцев назад

      You could add a status for each message to store whether it has expired (past the 24hr timeframe) or permanent. Or you could have the application calculate it based on the time it was sent.

  • @aalzahrani9271
    @aalzahrani9271 Год назад

    I have three users and each user has a table. How will I do that here ?

    • @DatabaseStar
      @DatabaseStar  Год назад

      Is there a way you can use a single table for all of your users, and each of the users have separate IDs?
      If not, then you could relate each of the three user tables in the same way as you see the user table in this design. It wouldn't be as robust, because you would have three foreign keys, each to a different table, but it could work.

    • @aalzahrani9271
      @aalzahrani9271 Год назад

      @@DatabaseStar What about creating users table as superclass and the other three tables that have been created will be subclasses?

  • @medilies
    @medilies Год назад

    In this case how to identify the sender?

    • @DatabaseStar
      @DatabaseStar  Год назад +1

      You can match the message.from_number to the contact.phone_number to see who the sender is.

  • @ron-95
    @ron-95 2 года назад

    hey can you make bridge table with 2 EAV data model?

  • @take_a_lunch
    @take_a_lunch 2 года назад

    Will searching or filtering messages slow down if more than a 100,000 messages are inserted into the database? What are the consequences of this, and how can it be resolved?

    • @DatabaseStar
      @DatabaseStar  2 года назад

      I don't think it would slow down. Databases are designed to handle large amounts of data, and even if you had millions of rows, the performance would still be manageable. You could use indexes to ensure the data can be filtered if needed, and there are other database-specific features that could be used.

    • @take_a_lunch
      @take_a_lunch 2 года назад

      @@DatabaseStar ohhhh....そか

    • @fuddyduddy
      @fuddyduddy 2 года назад +2

      @@take_a_lunch Don't pretend to be a Japanese. Chinese.

    • @nirajpatil2326
      @nirajpatil2326 2 года назад

      @@fuddyduddy damn 😂😂

  • @yertayyeras6160
    @yertayyeras6160 2 года назад +3

    So sorry, but I think it is bad design for chat. Because you wanna say, If I wanna open message list who wrote me or I, I need to check only column from_contact? but if another person wrote me, how should I get it?

    • @DatabaseStar
      @DatabaseStar  2 года назад +1

      Thanks for the feedback! If you want to see the list of messages that were sent to you, you can query the message table. If you want to display a list of your conversations, like the main screen in WhatsApp, you would query the conversation table and perhaps the contact table. If you want to see the messages in the conversation, you would query all tables but most of the data would be in the messages table.

    • @yertayyeras6160
      @yertayyeras6160 2 года назад

      @@DatabaseStar thank you for the explaining. But I have one question. What need to do, when one of the user deleted chat. It means this user deleted chat only for him/her. But second user should see old messages. Because first user deleted only for him/her sef

    • @DatabaseStar
      @DatabaseStar  2 года назад

      Good question. It would depend on how you want the application to behave. If you want to have this functionality, you could add a field to indicate whether the chat is deleted or hidden for a particular user. The chat would still exist, so other users can see it, as you mentioned.

    • @yertayyeras6160
      @yertayyeras6160 2 года назад

      @@DatabaseStar ohh, I got it. You didn’t add this functionality? It is my bad, I thought you added also this part, and I tried to understand that part, because, in my project I need this lol

  • @thongnguyen7354
    @thongnguyen7354 2 года назад

    How do i differentiate single chat and group chat? tks

    • @DatabaseStar
      @DatabaseStar  2 года назад

      This design assumes that a chat is the same regardless of how many participants. It could have two people (a single chat between two people) or three or more (a group chat).

    • @thongnguyen7354
      @thongnguyen7354 2 года назад

      @@DatabaseStar Thank you for answering my question, so how can I tell the difference?

    • @rahulraj94391
      @rahulraj94391 Год назад

      ​@@thongnguyen7354 get the count, if count. > 2 - it means it is a group else - it is a converrsation between only 2 people.
      Select Count(conversation_name) from conversation where conversation_id = "REQ ID".

    • @sandiladhikari7903
      @sandiladhikari7903 Месяц назад

      @@thongnguyen7354 for starters, you could see how many contact_id are there in group_member table, if its just 2, then its a single chat, if more than 2, the its actually what we generally understand as a group. Basically single chat between two people is also a group with only two participents. That is the concept. You could tell the difference only looking at participents.
      On a second approach, I don't see anything wrong with two table system for single chat and group chat feature.