Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davorminchorov's avatar

Live Chat App / Feature Database Design

Hey,

I have to add Customer Support feature in our project and one of the features of it is live chat. I am still researching around the whole idea.

The whole idea is that I want to allow users to join a room and chat. The chat would be 1-1 between the customer and the support representative but I want to allow for admins or developers or even other support representatives to join the room when needed (not always but let's say for emergency cases only).

So I started thinking about how I would save the chat messages in the database and it seems like a complicated stuff.

This is what I've come up with so far:

user Table 
id int primary
username varchar
// other fields 

room Table
id int primary
room varchar
timestamps
softdeletes

instant_message Table
id int primary
message text
timestamps
softdeletes

chat Table
id int primary
user_id int foreign references id on user 
instant message_id id foreign references id on instant_message
room_id id foreign references id on room
timestamps
softdeletes

I am not sure if this is a good design for a chat. The relationships between the models are:

One Chat - Many Rooms One Chat - Many Users One Chat - Many Instant Messages

but there is probably something wrong with this design and I don't know what it is!

I am trying to design the database to be flexible so I can get more info out of it if needed.

Does anyone know how I can improve this? Anyone ever had any experiences with building a chat app / customer support?

Thanks!

0 likes
9 replies
martinbean's avatar

@Ruffles Is there not an off-the-shelf solution that you could use? There are hundreds of live chat solutions out there, and this just feels like reinventing the wheel.

davorminchorov's avatar

Yeah, there might be some solutions and I will probably check out some of them and see if they'll fit my requirements.

jekinney's avatar

Instead of saving to a database which potentially will get huge fast with all that over head, I would suggest saving to a file or an excellent use of a nosql (mongo) database.

With nosql it just captures the conversation as a single document versus a few tables and potentially millions of rows.

davorminchorov's avatar

Those ideas were mentioned on Slack but I am curious, how will I save the conversation to MongoDb or even a file and read stuff from either storage?

Karunya's avatar

I have the same doubt as Mr.Ruffles one. How will I save the conversation to MongoDb or even a file and read stuff from either storage? could you guys please explain me about this.

luddinus's avatar

I would save those messages in your actual database (MySQL e.g)

I did something similar with 1-1 chats...

threads
----------
id
sender_id
recipient_id
// 

messages
-------------
id
thread_id
sender_id
read_at
//...

A conversation has two threads. One with the sender user "A" and recipient "B", and the other with the opposite. Then you add the message to both threads, and you could delete a message from the thread, but the other user still has it.

Of course it would be better but that's what I have at the moment

Please or to participate in this conversation.