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

thetanaz's avatar

Making chat functionality for my web app and not sure what to do regarding listening to messages

So this is my first time diving into reverb and I'm having quite a bit of trouble. The part of the logic that I'm struggling the most is how should I listen to new messages. Right now I'm listening for new messages only in the conversations that are being selected by the user, which is not viable, because they need to be able to receive notifications about new messages in other chats, as well as first messages in new conversations. However what I don't know is, if I should have 2 separate channels - 1 for the current conversation, and one for all conversations that are non-selected, or if I should just broadcast all the conversations and messages all at once. I am also paginating the conversations to 10 in my laravel backend and then sending them to my inertia react frontend, so let's say a user that's outside of the first 10 paginated conversations sends a message, how can I handle that?

0 likes
3 replies
vincent15000's avatar

Hello,

Here are some food for thought :

  • you can use only one channel or several channels, it depends on how you want to manage you chat, but it's better to have one channel per chat room, it will be easier to organize and handle back- and frontend

  • for each new message, you should broadcast the message via the websocket (Reverb) only to the authorized users, that means (probably) to all public channels and to the authorized private channels

  • if a user is not i the first 10 paginated conversations and sends a message, he sends a message in the certain channel (public / private) and then you broadcast the message to the authorized users, if the message is added to the 11th conversation, you can for example notificate the connected users that there is a new message in another conversation (green circle ? other icon ? to show the conversations with new messages)

thetanaz's avatar

@vincent15000 I think I figured it out. Here is what I did : Made an individual channel for every user that only they can listen to, so all messages from all conversations pass through that single channel.

Then I send 10 paginated conversations with 20 messages each to the front end for better initial loading (inertia and react). When a new message is being broadcast I add a isNewConversation boolean that is true if the length of the convo was 0 before that message was created. If it's a new conversation I fetch the conversation via an api endpoint , replace it as the top-most conversation and slice off the oldest conversation of the first page. This doesn't affect functionality, as clicking on page 2,3 etc reloads the page with fresh props, so the order remains the same.

If the message is not on the first page : I check by comparing the messages in state conversation ids to the conversation id of the newly sent message, and the isNewConversation is false, this means that the conversation is not new so the message must be beyond the first page, and I use again an API endpoint to fetch the conversation and put it on top of the conversation stack while slicing the oldest one off.

This seems to work quite well, I don't know about potential downsides yet.

1 like

Please or to participate in this conversation.