Mikegk
4 months ago
224
0
Laravel

Private channels won't work

Posted 4 months ago by Mikegk

Hey guys,

I just setup the laravel-websockets on my server. Now I got stuck on the change from regular to private channels (yes I know that presence channels are also nice and offer some more features, but at the moment I want to go with private channels).

So here are the files I use


//App\Events\ConversationEvent.php

namespace App\Events;

use App\User;
use App\Conversation;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ConversationEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $user;
    public $id_conversation;

    /**
     * Create a new event instance.
     *
     * @return void
     */
//    public function __construct(Conversation $conversation)
    public function __construct(User $user,$id_conversation)
    {
        $this->user = $user;
        $this->id_conversation = $id_conversation;
//        $this->dontBroadcastToCurrentUser();
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new PrivateChannel('Conversation.1');
    }
}

//routes\channels.php

use Illuminate\Support\Facades\Broadcast;

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

And the view simply looks like so

window.Echo.private('Conversation.1').listen('ConversationEvent', (e) => {
	console.log(e);
});

Everything is correctly set up I guess. If I change the Echo.private method to Echo.channel, I can see a connection in the console on "Conversation.1". If I switch back to Echo.private, It doesn't seem to work.

It's also not possible to trigger an Event through a basic route such as:

Route::get('/event',function(){
   event(new App\Events\ConversationEvent(Auth::user(),1));
});

The console doesn't display that event.

If it's important to know: I do use SSL, but Im not sure if that makes a difference here. And I also use the local "laravel-websockets" Library that simulates pusher as broadcaster.

Would be happy for any help.

Please sign in or create an account to participate in this conversation.