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>```