lundi 16 mars 2020

How to store unread messages count for group chat in Laravel app

I have read all the answers but still i did not get the answer which i am looking for. Here let me explain what i have and i have done so far.

I have created a chat application where user can have duo chat or a group chat. i have following database structure:

chatrooms

id | creator_id (who created chatroom)  | type (duo/group) |  group_id (if chatroom type is 'group')

messages

id  |  chatroom_id  |  senders_id  |  recipient_id  | message  |  read(by default 0)

I have one more table to keep track of chatroom participants

chatroom_user

id  | chatroom_id   | user_id 

Now everything is working fine for duo type chats. I am getting unread messages count for duo chat by running this query :

return auth()->user()
            ->chatrooms()
            ->withCount(['messages' => function (Builder $query) {
                $query->where('read', 0)->where('recipient_id', auth()->user()->id);
            }])
            ->get();

And this works perfectly fine:)

But now if i have group chat i have multiple recipients. i am not able to do what i have done above. So i created one more pivot table

message_recipient

id | message_id  |  recipient_id  | read

Now this way i can keep track of unread messages for specific user. But the problem is when i save a message i need to add here so many rows how many recipients are and that for each message. suppose i have a group with 10 people if somebody send message 'hello' i need to put 10 records in this table ( actually 10-1 ). So my question is: will it be a performance issue? Is there any better way to handle this kind of situation ?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire