Privsate channel doesn;t display messages - propably it's problem with authentication

31 Views Asked by At

I want to create private channel on Laravel + React with Pusher (I want to create chast online). I found issues like [here][1] and others but it doesn't solve my problem. Looks on my code.

  useEffect(() => {
    Pusher.logToConsole = true;

    const pusher = new Pusher("***", {
      cluster: "eu",
      broadcaster: "pusher",
      appId: "***",
      key: "***",
      secret: "***",
      auth: {
        headers: {
          Accept: "application/json",
          Authorization: "Bearer x9O***", //I added that manually becouse I'm using http only cookies
        },
      },
      authEndpoint: "http://localhost/broadcasting/auth",
    });

    const channel = pusher.subscribe("laravel-chat");
    channel.bind("message", function (data) {
      const jsonData = JSON.parse(JSON.stringify(data));
      setMessages((prevMessages) => [
        ...prevMessages,
        { message: jsonData.message },
      ]);
    });
  }, []);

I'm not sure that this code is corrently but it doesn't work. Here Authorization: "Bearer x9O***" I added token manually becouse I doesn't store my token in frontend. I'm using http only cookie instead. My Message event at this moment looks like this

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

    public $message;
    public $username;
    public function __construct($message, $username)
    {
        $this->message = $message;
        $this->username = $username;
    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array<int, \Illuminate\Broadcasting\Channel>
     */
    public function broadcastOn(): array
    {
        return [
            new PrivateChannel('laravel-chat'),
        ];
        // return ['laravel-chat'];
    }

    public function broadcastAs()
    {
        return 'message';
    }
}

I don't have any errors but this messages doesn't displat. Should you fix my code? [1]: How to use private channel in Laravel Pusher.js

0

There are 0 best solutions below