Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Database Design for storing Chat Messages between people

Tags:

I am trying to build a messaging/chat system. which can store conversation between two people in a chronological order. Also if User A deletes the conversation User B still should have access the conversation until he wishes to delete them.

  1. Inbox - All the messages recieved by the user from various users will be displayed with the latest message from that particular thread.

  2. Conversation Screen - Chronological order of the conversation between the User A and User B

This is the basic structure of the database i have come up with. Should i store the messages twice in the database ?

  1. id
  2. to_id
  3. from_id
  4. message
  5. timestamp
  6. read
like image 945
Harsha M V Avatar asked Nov 12 '11 16:11

Harsha M V


People also ask

Which database is best for storing messages?

MySQL is pretty mature. It's capable of storing and handling millions and millions of rows. NoSQL databases aren't magically more performant than relational databases.

Which type of database will be best for chat application?

I think it's hard to say that one database or another is the BEST without understanding more about the application but rest assured that MongoDB has been the choice for many popular chat applications.

How are messages stored in database?

You can store message by message in the DB having send to, send from and date time like fields, meaning one message per record. Or you can also save session based message history at once per record, and it may contain several messages per record.


2 Answers

I would use a lookup table for the messages that would store who has the rights to view that message

table->message                   |    table->messageUsers
id->0, message->'hi', user_id->1      user_id->1, message_id->0
                                      user_id->2, message_id->0

That way if a user deletes their message they are actually just deleting their relationship to the message not the message itself. you just remove them from the messageUsers table. or set a active field to 1 or 0.

like image 153
Tim Joyce Avatar answered Oct 09 '22 15:10

Tim Joyce


At first I thought that when one person deleted it you could just turn either To or From to null but that would make you lose who sent the message or to whom it was addressed.

You should just add a field deleted_by which will contain the id of the person who deleted it or will be null. So when selecting records from the inbox you have something like:

Select * From Messages where to_id = MyID and deleted_by <> MyID

when you delete the message you check if the deleted_by is null, if it is you update the deleted_by field with MyID, if it is not (means that the other party deleted it as well) you delete the record.

If you want to have the same functionality for threads instead of messages (i.e. manage the conversation and not one message at a time) you should have another table (MessageThreads) in which you have the from_id, to_id fields, deleted_by along with a thread_id field. in the Messages table you subsitute the from_id to_id and deleted_by with the thread_id.

like image 34
John Avatar answered Oct 09 '22 13:10

John