Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Erutan409's avatar

Attempting to create private channel without wildcard parameters

Per the title, I'm trying to configure a private channel without adding some ambiguous parameter with a {parameter} component in the channel name. Based on traversing the source code, and reading a SO answer, I should be able to achieve this with just prefixing the name with private-.

However, this doesn't seem to work when I try to connect to the channel with Laravel Echo. I'm thinking the issue is on the Reverb side, but I'm also wondering if I'm missing some nuanced config that isn't allowing this to work WITHOUT adding the aforementioned parameter in the channel name.

<?php

use App\Models\User;
use Illuminate\Support\Facades\Broadcast;

// necessary for sanctum authentication
Broadcast::routes(['middleware' => ['api', 'auth:sanctum']]);

// I'm forced to use this:
Broadcast::channel('resource.user.{id}', fn(User $user,  /* unused */ int $id) => $user->can('account.read'));

// I can forgo the $id parameter completely - just added to stress the difference

// instead of this:
Broadcast::channel('private-resource.user', fn(User $user) => $user->can('account.read'));

Laravel echo is sending the auth request correctly each time, regardless of the backend configuration (obviously). I'm not changing anything in the browser/JavaScript side.

Is this just something I have to do in order to establish a private channel that doesn't need additional parameters as part of the authentication?

0 likes
2 replies
LaryAI's avatar
Level 58

In Laravel, private channels are typically used to ensure that only authorized users can listen to certain events. When you define a private channel, you usually include a wildcard parameter to specify which users are allowed to listen to that channel. However, if you want to create a private channel without any wildcard parameters, you can do so by following these steps:

  1. Define the Channel:

    You can define a private channel without a wildcard parameter by simply using a static channel name. However, you need to ensure that the authorization logic is correctly set up to allow users to listen to this channel.

    use App\Models\User;
    use Illuminate\Support\Facades\Broadcast;
    
    // Define the private channel without wildcard parameters
    Broadcast::channel('private-resource.user', function (User $user) {
        return $user->can('account.read');
    });
    
  2. Configure Laravel Echo:

    On the client side, when using Laravel Echo, you need to ensure that you are connecting to the correct channel name. Since you are using a static channel name, you should connect to it directly:

    Echo.private('private-resource.user')
        .listen('YourEvent', (e) => {
            console.log(e);
        });
    
  3. Ensure Authentication:

    Make sure that your authentication middleware is correctly set up. Since you are using Sanctum, ensure that your API requests are authenticated properly. This is crucial because the private channel authorization will rely on the authenticated user.

  4. Check Broadcasting Configuration:

    Ensure that your broadcasting configuration is set up correctly in config/broadcasting.php. If you are using Pusher or another service, make sure the credentials and settings are correct.

  5. Debugging:

    If you encounter issues, check the network requests in your browser's developer tools to see if the authentication request is being made and if it is successful. Also, check the server logs for any errors related to broadcasting.

By following these steps, you should be able to create a private channel without using wildcard parameters. If you still face issues, it might be worth checking if there are any specific requirements or limitations with the broadcasting driver you are using (e.g., Pusher, Ably, etc.).

Erutan409's avatar

@LaryAI Well, I thought this automated response might have shed some light on what the problem might've been. I noticed that it showed prefixing the channel name in Echo.private('private-resource-user') with private-. However, as expected, it's just now in the channel name twice. So, that doesn't work at all.

Please or to participate in this conversation.