echo: laravel Broadcasting in private channels is not working.( cant subscribe to private channels automaticaly)
- Echo Version: 1.10.0
- Laravel Version: 8.4.0
- PHP Version: 7.4
- NPM Version: 6.14.6
- Node Version: 4.1.0
- pusher/pusher-php-server: 4.1.5
- pusher-js: 7.0.3
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>
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 22 (8 by maintainers)
Hey can you please re-share that repo ? I have the same issue
Okay I got it working. A couple of things:
pusher(see the docs): https://github.com/farshidahmadianpin/laravel-echo/blob/main/.env.example#L17['middleware' => ['auth']]here. Not sure why you added that. https://github.com/farshidahmadianpin/laravel-echo/blob/main/app/Providers/BroadcastServiceProvider.php#L17Hope that helps.
thank you very much. i remove this
['middleware' => ['auth']]and its working. thank you 🙏🙏🙏🙏🌺🌺🌸❤️Is the user actually logged into the application?
The linked support requests are only a day old. Please give it a few more days.
hey there @driesvints i try this problem in laracasts and stackoverflow and nobody cant answered me.
when in “App\Events\ChatBroad.php” i use “Channel” and in view script code i use “Echo.channel” it’s work properly. but if i use “PrivateChannel” in “App\Events\ChatBroad.php” and in view script code i use “Echo.private” it’s not working.
please help me https://laracasts.com/discuss/channels/laravel/laravel-broadcasting-broadcasting-in-private-chanel-not-working https://stackoverflow.com/questions/66447911/laravel-broadcasting-broadcasting-in-private-chanel-not-working-i-use-laravel-ec