Installation Add the package via Composer:
composer require edwin-luijten/ekko-broadcast
Publish the config file:
php artisan vendor:publish --provider="EdwinLuijten\EkkoBroadcast\EkkoBroadcastServiceProvider"
Configuration
Open config/ekko-broadcast.php and set your preferred broadcast driver (e.g., pusher, redis, log). Example for Pusher:
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'encrypted' => true,
],
First Use Case: Broadcasting an Event
Create an event (e.g., ChatMessageSent):
php artisan make:event ChatMessageSent
Update the event to use EkkoBroadcastEvent:
use EdwinLuijten\EkkoBroadcast\EkkoBroadcastEvent;
class ChatMessageSent extends EkkoBroadcastEvent
{
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn()
{
return new Channel('chat');
}
}
Fire the event in your controller:
event(new ChatMessageSent('Hello, world!'));
Frontend Setup Include the Ekko.js client in your Blade template:
<script src="https://cdn.jsdelivr.net/npm/ekko-broadcast@1.0.0/dist/ekko.min.js"></script>
Initialize the client:
const ekko = new Ekko({
key: 'YOUR_PUSHER_KEY',
cluster: 'YOUR_CLUSTER',
encrypted: true
});
ekko.subscribe('chat', (message) => {
console.log('New message:', message);
});
// In your controller or service
event(new OrderShipped($orderId));
// In your event
class OrderShipped extends EkkoBroadcastEvent
{
public $orderId;
public function __construct($orderId)
{
$this->orderId = $orderId;
}
public function broadcastOn()
{
return new Channel('orders');
}
public function broadcastAs()
{
return 'order.shipped';
}
}
chat, notifications, orders).// Subscribe to multiple channels
ekko.subscribe(['chat', 'notifications'], (message) => {
console.log('Received:', message);
});
class PrivateMessageSent extends EkkoBroadcastEvent
{
public $userId;
public $message;
public function __construct($userId, $message)
{
$this->userId = $userId;
$this->message = $message;
}
public function broadcastOn()
{
return new PrivateChannel('user.' . $this->userId);
}
}
ekko.subscribe('user.123', (message) => {
console.log('Private message:', message);
});
class UserJoinedRoom extends EkkoBroadcastEvent
{
public $roomId;
public $userId;
public function __construct($roomId, $userId)
{
$this->roomId = $roomId;
$this->userId = $userId;
}
public function broadcastOn()
{
return new PresenceChannel('room.' . $this->roomId);
}
}
ekko.subscribe('room.123', (message) => {
console.log('Room update:', message);
});
// Join the presence channel
ekko.join('room.123', { userId: 123, name: 'John' });
npm install --save laravel-echo pusher-js
Configure resources/js/bootstrap.js:
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
forceTLS: true,
authEndpoint: '/broadcasting/auth',
});
Subscribe to Ekko channels via Echo:
Echo.join(`ekko.${channelName}`)
.here(users => console.log(users))
.joining(user => console.log('joining:', user))
.leaving(user => console.log('leaving:', user));
namespace App\Http\Middleware;
use Closure;
use EdwinLuijten\EkkoBroadcast\Middleware\BroadcastMiddleware;
class AuthenticateEkko extends BroadcastMiddleware
{
public function handle($request, Closure $next)
{
if (!auth()->check()) {
return false;
}
return $next($request);
}
}
EkkoBroadcastServiceProvider:
$this->app['ekko']->extend('private', function ($app) {
return \EdwinLuijten\EkkoBroadcast\PrivateChannel::class;
});
$this->app['ekko.private.middleware']->append(AuthenticateEkko::class);
broadcastWith method in your event to customize the payload:
public function broadcastWith()
{
return [
'message' => $this->message,
'timestamp' => now()->toDateTimeString(),
'user' => auth()->user()->only('id', 'name'),
];
}
log driver for development/testing:
'driver' => env('APP_ENV') === 'local' ? 'log' : 'pusher',
storage/logs/laravel.log.$this->app->instance('ekko', \Mockery::mock('overload:EdwinLuijten\EkkoBroadcast\EkkoBroadcastManager'));
$broadcastMock->shouldReceive('broadcast')
->once()
->with('chat', 'Hello, world!');
BroadcastAuthorization trait or custom middleware (as shown above).How can I help you explore Laravel packages today?