Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

finleth's avatar

Laravel API Broadcast Installation

I was looking at Laravel Reverb today and noticed the php artisan install:broadcasting command doesn't work on an API installation of Laravel. The error received is:

copy(/var/www/html/resources/js/echo.js): Failed to open stream: No such file or directory

Which makes sense since the frontend files no longer exist in the API. Is there a way to install the broadcasting functionality minus the frontend portion? Or is this something that's not even wise to pursue since it's really meant to be used in conjunction with a coupled frontend?

Jeffrey

0 likes
4 replies
LaryAI's avatar
Level 58

Hello Jeffrey,

The php artisan install:broadcasting command is part of the Laravel UI package, which is intended for applications that have a traditional web front-end. Since you're working with an API installation of Laravel, you won't have the frontend scaffolding that the command expects to be present.

However, you can still use Laravel's broadcasting capabilities in an API-centric application. You just need to set up the necessary backend components manually. Here's how you can do it:

  1. Install Laravel Websockets (optional): If you're not using Pusher and prefer a self-hosted solution, you can use the laravel-websockets package. Install it via Composer:
composer require beyondcode/laravel-websockets
  1. Configure Broadcasting: Open your .env file and set the BROADCAST_DRIVER to pusher or redis, depending on your preference:
BROADCAST_DRIVER=pusher
  1. Configure the config/broadcasting.php file: Make sure the configuration matches your chosen driver settings. If you're using Pusher, you'll need to provide your Pusher app credentials:
'connections' => [
    // ...
    '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'),
            'useTLS' => true,
        ],
    ],
    // ...
],
  1. Create Events: Create event classes that implement the ShouldBroadcast interface. This is how you define which events should be broadcasted:
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

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

    public $data;

    public function __construct($data)
    {
        $this->data = $data;
    }

    public function broadcastOn()
    {
        return new Channel('example-channel');
    }
}
  1. Broadcast Events: Dispatch events as you normally would in your application:
event(new ExampleEvent($data));
  1. Client-Side Setup: On the client side, you'll need to set up a way to listen to the events. This will depend on the frontend technology you're using. For example, if you're using JavaScript, you can use Laravel Echo with Pusher.js or a similar library to listen for events.

Since you don't have the frontend scaffolding, you'll need to include the necessary scripts in your frontend application manually. Here's an example using Laravel Echo with Pusher:

import Echo from 'laravel-echo';
import Pusher from 'pusher-js';

window.Pusher = Pusher;

let echo = new Echo({
    broadcaster: 'pusher',
    key: 'your-pusher-key',
    cluster: 'your-pusher-cluster',
    encrypted: true
});

echo.channel('example-channel')
    .listen('ExampleEvent', (e) => {
        console.log(e);
    });

Remember to replace 'your-pusher-key' and 'your-pusher-cluster' with your actual Pusher credentials.

By following these steps, you can set up broadcasting in your Laravel API without the need for the frontend files that the php artisan install:broadcasting command tries to publish.

Snapey's avatar

you can ignore 100% of Lary's advice as it is based on information before 2022 and knows nothing of reverb and installing broadcasting

2 likes
karu's avatar

@Snapey so how to install it on a API project? i run Laravel 10 too....

Please or to participate in this conversation.