hybrid1969's avatar

Laravel 10 Pusher Broadcast sent as private but shows as prescence?

Ok really odd one, I am sending the event as a private channel but looking at posher debug it displays as a Prescence chanel no matter what I set it too. Any ideas what is causing this as I can send from pusher debug no issue.

where is it changing/deciding the channel should be presence and not private.

channels.php
Broadcast::channel('{tenant}', function ($user, $tenant) {
    // Adjust the authorization logic as per your needs
    return (int) $user->tenant_id === (int) $tenant;
});
NewEvent.php
<?php

namespace App\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;

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

    public $tenant;
    public $message;

    /**
     * Create a new event instance.
     */
    public function __construct($tenant,$message)
    {
        $this->tenant = $tenant;
        $this->message = $message;

        //error_log(str_replace('tenant', '', DB::connection()->getDatabaseName()));
        //error_log(request()->msg);
    }

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

    public function broadcastAs()
    {
        return 'NewEvent';
    }
}
index.blade.php
<script>
            // Enable pusher logging - don't include this in production
            Pusher.logToConsole = false;

            var pusher = new Pusher('{{ env('PUSHER_APP_KEY') }}', {
                cluster: '{{ env('PUSHER_APP_CLUSTER') }}',
                // encrypted: true,
                // authEndpoint: '/broadcasting/auth',
                auth: {
                   headers: {
                       'X-CSRF-TOKEN': document.head.querySelector('meta[name="csrf-token"]').content,
                //         Authorization: `Bearer ${localStorage.getItem('access_token')}`, // Adjust according to your auth method
                   },
                },
            });

            var channel = pusher.subscribe('{{ str_replace('tenant', '', DB::connection()->getDatabaseName()) }}');
            channel.bind('NewEvent', function(data) {
                // Do what you want with the data
                alert(JSON.stringify(data));
                console.log(data);
            });
        </script>```
0 likes
2 replies
hybrid1969's avatar

so if i send from pusher debug console i get -

API message Channel: private.24ff089a-e246-43df-b1e4-e0a4e8178188, Event: NewEvent
{
  "msg": "...."
}

and yet when i send from laravel using the above code i get -

API message Channel: presence-24ff089a-e246-43df-b1e4-e0a4e8178188, Event: NewEvent
{
  "tenant": "24ff089a-e246-43df-b1e4-e0a4e8178188",
  "message": "itworks"
}
hybrid1969's avatar
hybrid1969
OP
Best Answer
Level 2

Ok so while trying to make other changes i changed the Event __construct to this which for some opdd reason fixed the issue. unsire if its related but just adding here for others stuck with this-

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

        // Specify the connection and queue
        $this->connection = 'redis'; // Specify the connection
        $this->queue = 'pusher'; // Specify the queue
    }```

Please or to participate in this conversation.