1

I have spend many hours to solve this issue of mine, reading the doc multiple times, googling here and there: SO, Laracast, Larachat, etc, but still couldn't get Laravel Echo to subscribe to Pusher presence channel, and it doesn't show any error in console tab

Public and Private channel are working fine and smooth, users can subscribe, users can listen / trigger event(s)

Note: before creation of this post, I have search questions related to my current issue, none of them have answer

Some questions similar to mine:

Spec:

  • Laravel: 7.30.1
  • laravel-Echo: 1.10.0 (latest; atm)
  • pusher/pusher-php-server: 4.0
  • pusher-js: 7.0.3 (latest; atm)

In resource/js/bootstrap.js

import Echo from 'laravel-echo'

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

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    cluster: process.env.MIX_PUSHER_APP_CLUSTER,
    forceTLS: true,
    authEndpoint: '/api/broadcasting/auth',
    auth: {
        headers: {
            'Authorization': `Bearer ${localStorage['token']}`
        }
    }    
});

In routes/api.php

// https://stackoverflow.com/questions/55555844/authorizing-broadcasting-channel-in-an-spa
Route::post('/broadcasting/auth', function (Request $request) {
    $pusher = new Pusher\Pusher(
        env('PUSHER_APP_KEY'),
        env('PUSHER_APP_SECRET'),
        env('PUSHER_APP_ID'),
        [
            'cluster' => env('PUSHER_APP_CLUSTER')
        ]
    );

    // This will return JSON response: {auth:"__KEY__"}, see comment below
    // https://pusher.com/docs/channels/server_api/authenticating-users
    $response = $pusher->socket_auth($request->request->get('channel_name'), $request->request->get('socket_id'));

    return $response;
})->middleware('auth:sanctum');

In routes/channels.php

// https://laravel.com/docs/8.x/broadcasting#authorizing-presence-channels
Broadcast::channel('whatever', function ($user) {
    return [
        'id' => $user->id,
        'name' => $user->name
    ];
});

In home.vue

...
...
created() {
    Echo.join('whatever') // DOES NOT WORK, Even in mounted() vue lifehook, and in Pusher dashboard, it doesn't show this channel name
        .here((users) => {
            console.table(users)
        })
}

Q: Why Laravel Echo not subscribing to Pusher presence channel? and even in Pusher, it doesn't show channel name: presence-whatever, only disconnected (after I refreshed the page) and then connected like nothing happen

Thanks in advance

2
  • And what's your event's broadcastOn() function? Mar 9, 2021 at 12:53
  • Hi, public channel and private channel are working fine, the problem is: I'm tyring to subscribe on presence channel, but it's not working like any tutorial / doc, I'm not sending any data, just want to subscribe to presence channel
    – Gwein
    Mar 9, 2021 at 14:52

3 Answers 3

1

1. Make sure to set the broadcast driver to .env after that do php artisan config:clear BROADCAST_DRIVER=pusher

2. Ferify your auth.php. I have like this

enter image description here

3. Check your broadcasting.php this is my dev config

enter image description here

4. Check CORS settings I installed fruitcake/laravel-cors and this is my cors.php config enter image description here

Maybe for you supports_credentials need to be true

5. Check your channels.php. I have

enter image description here

3
  • Hi maxim, I thank you for the answer, but unfortunately, atm my pc is broken and need to be fixed, at least 2 week, but don't worry, after my pc got fixed, I will re-open the bounty and test your answer, if it works, I will check your answer as the correct one, again, I'm sorry and please wait for my pc to be fixed
    – Gwein
    Mar 18, 2021 at 5:55
  • Hello maxim, I just tested your answer on my friend's PC, and it's not working
    – Gwein
    Mar 19, 2021 at 14:14
  • @ChristianDelvianto there are some errors? Also in channels.php you probably need to return user data or false (if access denied) Mar 20, 2021 at 8:17
0

Make sure to set the broadcast driver to env after that do php artisan config:clear

Use window.Echo.channel('whatever') to join the channel.

2
  • Public and private channel are working fine, the problem is, user are not subscribing to presence channel
    – Gwein
    Mar 9, 2021 at 14:59
  • 1
    Hi @musti, I'm trying to subscribe to presence channel, not sending any data tho, normally, without any route for channel in routes/channel.php we can subscribe to a channel / multiple channel, as long as we have syntax in our code like: Echo.channel('public') or Echo.private('private'), and we can see in Pusher dashboard that we are subscribing to channel(s), I have tested it, and it's working for me, in terms: public and private channel
    – Gwein
    Mar 10, 2021 at 7:53
0

Had the exact same issue and was stuck for hours. I changed 'useTLS' to FALSE in config/broadcasting.php and magically got it working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.