Installation And Configuration

Install Larasocket composer package and npm package and laravel-echo package, then configure.

To bring in Larasocket to use as your broadcaster, you can easily install it via composer:

composer require larasocket/larasocket-driver

The next thing you need to do for Larasocket is to update your .env file. You will need to change your broadcast driver from its default “log” value to Larasocket and replace <token> with the token you created in Larasocket website for LARASOCKET_TOKEN variable.

BROADCAST_DRIVER=larasocket
LARASOCKET_TOKEN=<token>
MIX_LARASOCKET_TOKEN="${LARASOCKET_TOKEN}"

All of your application's event broadcasting configuration is stored in the config/broadcasting.php configuration file. You need to add the Larasocket driver as an option in connections array in this file.

'larasocket' => [
    'driver' => 'larasocket',
    'token' => env('LARASOCKET_TOKEN'),
]

For any Laravel application that uses broadcasting, you will first need to register the App\Providers\BroadcastServiceProvider. In new Laravel applications, you only need to uncomment this provider in the providers array of your config/app.php configuration file. This BroadcastServiceProvider contains the code necessary to register the broadcast authorization routes and callbacks.

Finally, you are ready to install and configure Laravel Echo, which will receive the broadcast events on the client-side. Laravel Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by your server-side broadcasting driver. You may install Echo via the NPM package manager. In this example, we will also install the larasocket-js to connect client to broadcaster. What the larasocket-js will do is making sure that Echo provides Larasocket as you broadcast adaptor.

npm i laravel-echo larasocket-js

Once Echo is installed, you are ready to create a fresh Echo instance in your application's JavaScript with Larasocket broadcaster. A great place to do this is at the bottom of the resources/js/bootstrap.js file that is included with the Laravel framework.

import Echo from 'laravel-echo';
import Larasocket from 'larasocket-js';
window.Echo = new Echo({
    broadcaster: Larasocket,
    token: process.env.MIX_LARASOCKET_TOKEN,
});

Last updated