0

I am making a laravel pusher chatting app I have this function to store messages here i am triggering

Event: event(new MessageSent($request->message, (int) $request->user()->id, $receiverId));

public function store(Request $request, ?int $receiverId = null)
    {
        $request->validate([
            'message' => 'required|string',
        ]);
        if (empty($receiverId)) {
            return;
        } 
        try {
            event(new MessageSent($request->message, (int) $request->user()->id, $receiverId));
            $this->chat->sendMessage([
                'sender_id' => (int) $request->user()->id,
                'receiver_id' => $receiverId,
                'message' => $request->message,
            ]);
            return Redirect::route('chat.index', $receiverId);
        } catch (\Throwable $th) {
            return Redirect::route('chat.index', $receiverId);
        }
    }
This is how i am calling it from frontend which uses react intertia
    const submit = (e) => {
        e.preventDefault();

        axios.defaults.withCredentials = true;
        axios.get(hostName + "sanctum/csrf-cookie").then(
            (response) => {
                post(route('chat.store', { receiverId: userChat.receiverID }))
                
                Echo.private(`messenger.${user.id}.${userChat.receiverID}`)
                    .listen('.chat', (e) => {
                      
                        setUserChat((prevUserChat) => ({
                            ...prevUserChat,
                            messages: [...prevUserChat.messages, e],
                          }));

                    });
                setData('message', '');
            }).catch((err) => { 
                console.log(err);
            });
    };

This is my event

public function broadcastOn(): Channel
    {
    return new PrivateChannel('messenger.' . $this->senderId . '.' . $this->receiverId);
    }

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

When i send message it works fine the event is fired in laravel-websockets admin panel but problem is that when i try logging in from two differnet accounts in differnt browser window and send message from User 1 to User 2 than it is shown in user 1 UI but it is not received in User 2 UI. May be i have to send message to that specifi receiver id.

0 Answers0