I am trying to broadcast a channel for my client application from the lumen app using the laravel echo server. here I can successfully broadcast a public channel but when I try to broadcast a private channel my laravel echo server throw en error
Error authenticating qEWAcyHQxWrbCvFmAAAA for private-example-channel Error: Invalid URI "http:localhost:85/broadcasting/auth"
I have checked my steps and found now the issue logically but here is something which I am might be missing.
what I'm doing for now is I have registered the broadcast service in the bootstarp/app.php file create event and provider and call that event from controller then init laravel echo server from lumen app and laravel echo + socket.io client for the client-side app.
// laravel-echo-server.json
{
"authHost": "http:localhost:85",
"authEndpoint": "/broadcasting/auth",
"clients": [],
"database": "redis",
"databaseConfig": {
"redis": {},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "localhost",
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
},
"apiOriginAllow": {
"allowCors": false,
"allowOrigin": "",
"allowMethods": "",
"allowHeaders": ""
}
}
//client side
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
auth: {
headers: {
Authorization: `Bearer ${token}`
}
}
})
// window.Echo.channel('example-channel')
window.Echo.private('example-channel')
.listen('NewMessageEvent', (e) => {
console.log('hello', e)
}).error(err => console.log('error', err))
//bootstrap/app.php
$app->register(Illuminate\Broadcasting\BroadcastServiceProvider::class);
$app->register(Illuminate\Redis\RedisServiceProvider::class);
$app->register(App\Providers\EventServiceProvider::class);
// web.php
$router->group(['middleware' => ['jwt.auth'], 'prefix' => 'api'], function () use ($router) {
Broadcast::channel('example-channel', function () {
Log('chennal called');
return true;
});
});
//NewMessageEvent
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Queue\SerializesModels;
class NewMessageEvent extends Event implements ShouldBroadcast
{
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
//
$this->message = $message;
// $this->channel = $channel;
}
public function broadcastOn()
{
// return new Channel('example-channel');
return new PrivateChannel('example-channel');
}
public function broadcastWith()
{
$data = $this->message;
return ["data" => $data];
}
}
//class NewMessageListener
<?PHP
namespace App\Listeners;
use App\Events\NewMessageEvent;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
class NewMessageListener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Handle the event.
*
* @param NewMessageEvent $event
* @return void
*/
public function handle(NewMessageEvent $event)
{
//
}
}
//trigger event
event(new \App\Events\NewMessageEvent('this is the message from the event catch it if you can'));
