pusher / pusher-js

Pusher Javascript library
http://pusher.com
MIT License
2.11k stars 374 forks source link

pusher and laravel-echo cant subscribe automaticaly to private channel #536

Closed farshid-ahmadian closed 3 years ago

farshid-ahmadian commented 3 years ago

Description:

I use pusher and laravel-echo to create chat app in laravel. public Channels work properly.

I use pusher.com debug console and I realized in PrivateChannel laravel-echo can't subscribe users to Available private channels automaticaly. notice that commented codes is for public channel.

App\Events\ChatBroad.php is:

    public function broadcastOn()
    {
        //        return new channel('recieve-chat.' . $this->chat['reciever_id']);
        return new PrivateChannel('recieve-chat.' . $this->chat['reciever_id']);
    }

routes/channels.php is:

Broadcast::channel('recieve-chat.{reciever_id}', function ($user , $reciever_id) {
      return true;
   // return $user->id == $reciever_id;

});

and in App\Providers\BroadcastServiceProvider i use this middleware:

Broadcast::routes(['middleware' => ['auth']]);

bootstrap.js is:

import Echo from 'laravel-echo'

window.Pusher = require('pusher-js');

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: '83b97539662b786ffabb',
    cluster: 'eu',
    forceTLS: true,
    authEndpoint: '/broadcasting/auth',
    csrfToken: $('meta[name="csrf-token"]').attr('content'),
});

blade view file:

        <script src="{{asset('js/app.js')}}"></script>
        <script>
            {{--window.addEventListener('DOMContentLoaded' , function () {--}}
            {{--    Echo.channel('recieve-chat.{{Auth::id()}}' )--}}
            {{--        .listen('ChatBroad', (e) => {--}}
            {{--            window.livewire.emit('recieve:' + e.chat.bid_id , e.chat);--}}
            {{--            $(chatScrollDown('.chat'+ e.chat.bid_id ));--}}
            {{--        });--}}
            {{--});--}}
        window.addEventListener('DOMContentLoaded' , function () {
            Echo.private('recieve-chat.{{Auth::id()}}' )
                .listen('ChatBroad', (e) => {
                    window.livewire.emit('recieve:' + e.chat.bid_id , e.chat);
                    $(chatScrollDown('.chat'+ e.chat.bid_id ));
                });
        });
    </script>
benw-pusher commented 3 years ago

Do you know if your auth endpoint is being correctly invoked? Do you have any logging there? Are any error messages returned in your client when subscribing to the private channels?

farshid-ahmadian commented 3 years ago

@ben-pusher hey there, yes I checked all of this. i explained all of my codes in here i will be gratefull to helpme

benw-pusher commented 3 years ago

I can't see any information there about logging on your auth endpoint and what is happening with the authentication request at your server side. My suspicion is that the authentication for the private channel subscription is failing.

farshid-ahmadian commented 3 years ago

@ben-pusher hey there, if auth endpoint has problem. irecieve 500 error in console. i checked all of this. and i describe the problem and some additional data in here . thank you 🌺

farshid-ahmadian commented 3 years ago

in App\Providers\BroadcastServiceProvider.php

 public function boot()
    {
        Broadcast::routes();

        require base_path('routes/channels.php');
    }

we dont need any middleware!!