Laravel Broadcast Event Private

I have been trying tog et private channels working with Laravel, and laravel-websockets

I got past a few hurdles, but the remaining one is on trying to auth:

preg_match(): Compilation failed: unrecognized character after (?< at offset 13

That error seems to be related to:

 …/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php150 

I guess the relavant pieces of code would be the echo config and listen.

bootstrap.js

window.Echo = new Echo({
    broadcaster: 'pusher',
    key: process.env.MIX_PUSHER_APP_KEY,
    encrypted: false,
    wsHost: window.location.hostname,
    wsPort: 6001,
    disableStats: true,
});

admin.blade.php

 Echo.private('messages.{{Auth::id()}}')
    .listen('NewMessageNotification', (e) => {
      console.log(e.message.message);
});

Just in case, channels.php

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

If I remove the private, everything works.

1 answer

  • answered 2019-07-10 22:06 TDawg

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

    });

    The problem was the $ in the channel...

    This is the correct way

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