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

Greeenone's avatar

Conversation using pusher - Need some advice and review

Hey everyone,

I'm creating a "chat app" using Laravel, Pusher and VueJS.

I created a working private conversation between two users and I need some code review. Everything works but I'm not sure if is it the correct approach for that

My Event looks like this:

class MessageSent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public ConversationsMessages $message;

    public function __construct(ConversationsMessages $message)
    {
        $this->message = $message;
    }

    public function broadcastOn(): Channel
    {
        return new Channel('private_chat.' . $this->message->conversation_id);
    }

    public function broadcastAs(): string
    {
        return 'MessageSent';
    }
}

And my Broadcast looks like this:

Broadcast::channel('private_chat.{conversationId}', function ($user, $conversationId) {
    $conversation = Conversations::with('participants')->find($conversationId);

    if (!$conversation) return false;

    return $conversation->participants->contains($user->id);
});

My message app contain also a sidebar with all started conversation. It contain

  • Name
  • Last message
  • Last message date

I would like to update last message if user receive an message. What will be the best approach for that?

Create a new event that listen for all conversations that relate to user? Or maybe edit current event to handle both cases?

0 likes
1 reply
LaryAI's avatar
Level 58

Your current implementation for broadcasting messages in a private chat using Laravel, Pusher, and VueJS looks solid. However, to address your requirement of updating the sidebar with the last message, you can extend your current approach slightly.

Solution

  1. Modify the MessageSent Event:

    • Add a new property to hold the conversation details.
    • Update the constructor to accept and set this new property.
  2. Update the VueJS Component:

    • Listen for the MessageSent event.
    • Update the sidebar with the new message details.

Updated MessageSent Event

Update the Broadcast Channel

Ensure that the user is authorized to listen to the channel:

Broadcast::channel('private_chat.{conversationId}', function ($user, $conversationId) {
    $conversation = Conversations::with('participants')->find($conversationId);

    if (!$conversation) return false;

    return $conversation->participants->contains($user->id);
});

VueJS Component

In your VueJS component, listen for the MessageSent event and update the sidebar accordingly.

Echo.channel('private_chat.' + conversationId)
    .listen('.MessageSent', (e) => {
        // Update the chat window with the new message
        this.messages.push(e.message);

        // Update the sidebar with the last message details
        let conversation = this.conversations.find(c => c.id === e.conversation.id);
        if (conversation) {
            conversation.last_message = e.message.content;
            conversation.last_message_date = e.message.created_at;
        }
    });

Controller Update

When you dispatch the MessageSent event, ensure you pass the conversation details:

public function sendMessage(Request $request)
{
    $message = new ConversationsMessages();
    $message->conversation_id = $request->conversation_id;
    $message->user_id = auth()->id();
    $message->content = $request->content;
    $message->save();

    $conversation = Conversations::with('participants')->find($request->conversation_id);

    broadcast(new MessageSent($message, $conversation))->toOthers();

    return response()->json(['status' => 'Message Sent!']);
}

Summary

By extending the MessageSent event to include conversation details and updating your VueJS component to handle these details, you can efficiently update the sidebar with the last message information. This approach keeps your code clean and maintains the separation of concerns.

Please or to participate in this conversation.