Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Laravel Echo PresenceChannel vs listen events doesn't work #599

Open
Saberqadimi opened this issue Aug 14, 2022 · 2 comments
Open

Laravel Echo PresenceChannel vs listen events doesn't work #599

Saberqadimi opened this issue Aug 14, 2022 · 2 comments

Comments

@Saberqadimi
Copy link

Good time, for the implementation of the chat room, I have done all the Pusher configurations correctly, and sending messages and displaying online users works correctly, but the event in the .listen method does not work, and it does not even log. Thank you for your help. I have been dealing with this problem for a long time. I hit
this is my event
`class MessageSended implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets;

public $message;

/**
 * Create a new event instance.
 *
 * @return void
 */
public function __construct(Message $message)
{
    $this->message = $message;
}


public function broadcastWith()
{

    $this->message->load('user');
    return [
        'message' => array_merge($this->message->toArray(), [
            'selfOwned' => false
        ])
    ];
}
/**
 * Get the channels the event should broadcast on.
 *
 * @return \Illuminate\Broadcasting\Channel|array
 */
public function broadcastOn()
{
    return new PresenceChannel('chat');
}

}`

and in echo.js

`import Bus from './bus'
window.Echo.join('chat')

.listen('.MessageSended', (e) => {
console.log(e);
// Bus.$emit('message.added', e.message)
})
`

and this Channel
Broadcast::channel('chat', function ($user){
return [
'id' => $user->id,
'name'=> $user->name
];

});
`

and this message controller
` public function store(Request $request)
{

    $message = auth()->user()->messages()->create([
        'body'=> $request->body
    ]);

     broadcast(new MessageSended($message))->toOthers();

    return response()->json($message, 200);
}`
@KhalidHimmi
Copy link

Hello
Have you found the solution? I am stuck with the same problem for days.
Could you share the code here on github,if you have solved the issue.

@Saberqadimi
Copy link
Author

Saberqadimi commented Oct 11, 2022 via email

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants