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

webleyson's avatar

Chat App with 2 different user types.

Hello peeps. I'm losing my cool trying to figure out the most scalable schema for a chat app with 2 different user types.

User and Provider (Admin) Ive been trying to use this as a template as it incorporates group messaging (The provider account may send a message to many users - in a group) https://www.tome01.com/efficient-schema-design-for-a-chat-app-using-postgresql

I did try it using a polymorphic pivot table but thats just causing me more headaches. Can someone provide a scalable solution please? A user will have conversations? A conversation will have all the messages Will it work the same for the Provider? And I guess I need to record who sent the message right?

So far I have something like this

Conversations id provider_id user_id

messages conversation_id content sender_type

How would the pivot table work? Would I need one for each User type?

Thanks

0 likes
2 replies
martinbean's avatar

@webleyson I try and stay away from multiple user types as much as possible; they cause nothing but problems. It’s better to use roles instead to designate things like whether a user is an administrator or not.

For conversations, I usually add a belongs-to-many “participants” relation. That way, you can support single user-to-user conversations and group conversations easily. A conversation then has many messages, and each message belongs to a user (the author of that message). That way, messages aren’t tied to participants. If a participant leaves a conversation, then all of their messages will remain and still show as “Posted by John Doe” or whatever, even if John Doe has since left the conversation.

webleyson's avatar

Thanks for the reply. My whole system is based on different 'guards'.. We have users who are end customers. And provider accounts who run their business. They're totally different. Having said that, any other suggestions?

Please or to participate in this conversation.