0

I have a Laravel application that uses Echo and Pusher to provide real-time chat functionality. The application has three chat rooms: welcome, casual, and serious. All users can access these chat rooms.

Currently, my chat component is set up like this:

mounted() {
    window.Echo.channel('room-chat.' + this.room.unique_id)
        .listen('.newMessage', (e) => {
            this.$inertia.reload({preserveScroll: true})
        });
}

My NewMessageEvent:

class NewChatMessage implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets;

    public $roomMessage;

    public function __construct(RoomMessage $roomMessage)
    {
        $this->roomMessage = $roomMessage;
    } 

    public function broadcastOn(): array
    {  
        return [
            new Channel('room-chat.'.$this->roomMessage->room_uid),
        ];
    }

        public function broadcastAs()
    { 
        return 'newMessage';
    }
}


My channels.php

Broadcast::channel('room-chat.{room_uid}', function() {
    return true;
});

My Controller:

    public function home_chat_send(Request $request,$group_uid){
        $request->validate([
            'text'=>'required|max:500'
        ]); 

        $message = RoomMessage::create([
            'room_uid'=>$group_uid,
            'user_id'=>Auth::user()->id,
            'content'=>$request->text
        ]);

        broadcast(new NewChatMessage($message))->toOthers(); 
        return back();
    }

The problem I'm facing is that for example when a user sends a message in casual room, users in the other chat rooms also receives the events with $inertia.reload().

I modified my component to throw errors by doing this:

        window.Echo.private('room-chat.casual').listen('.newMessage', (e) => 
        {  
            this.$inertia.post() //this is to throw the error 
        });

Im using my phone to send messages in Casual room, and my laptop which was in /welcome chat room still got the error.

Can someone help me? I think i missed something but im not sure

Jeb
  • 3
  • 1
  • 3

0 Answers0