jilenloa/echo-socketcluster-broadcast-driver

Laravel socketcluster broadcaster. This is compatible with Laravel Echo client

dev-master 2023-03-07 01:16 UTC

This package is auto-updated.

Last update: 2024-05-07 03:52:30 UTC


README

Build Status Total Downloads

It's supported on PHP 7.1+, Laravel 5.8+

Before you can use this library, you first need to setup a server "echo-socketcluster-server". It works similarly to the default "laravel-echo-server". However, it is installed when you run the vendor:publish command. Find that below.

php artisan vendor:publish --tag=echosocketcluster

Echo Compatible Server using SocketCluster

The echo-socketcluster-server package is created using SocketCluster and it's part of this repo and you can copy it to any folder of your choice. It comes with its own package.json, so you would need to run the npm install command inside the folder afterwards.

For more information on SocketCluster, visit https://socketcluster.io/.

Laravel Installation

Install package using Composer

composer require jilenloa/echo-socketcluster-broadcast-driver:dev-master

Add below to your broadcasting.php file.

'echosocketcluster' => [
    'driver' => 'echosocketcluster',
],

Add this configuration to your .env file. Update the current setting for BROADCAST_DRIVER to the one below

BROADCAST_DRIVER=echosocketcluster #"redis" is also compatible with this broadcast server just like the default laravel-echo

ECHO_SC_HOST=localhost:8001
ECHO_SC_TOKEN=echo-server-token
REDIS_KEY_PREFIX=laravel_ # just like you would set in laravel-echo-server.json
SOCKETCLUSTER_PORT=8001
SOCKETCLUSTER_WS_ENGINE=uws # defaule engine is "ws"
SOCKETCLUSTER_HTTP_TOKEN=echo-server-token

If you have not done this, you can publish the configuration and echo-socketcluster-server files using the command:

php artisan vendor:publish --tag=echosocketcluster

You can then later update the package later using composer:

composer update

Laravel Echo Client

npm install laravel-echo
npm install laravel-echo-connector-socketcluster

Below is a sample use of the Laravel Echo client.

import Echo from "laravel-echo";
import SocketClusterConnector from "laravel-echo-connector-socketcluster";
window.socketClusterClient = require('./socketcluster-client');

let echo = new Echo({
    client: socketClusterClient,
    broadcaster: SocketClusterConnector,
      auth: {
        headers: {
            //add custom headers here, useful for JWT authentication
        },
        hostname: 'localhost:8001', //laravel host to authorize channels. this is sometimes optional
      },
      socketcluster: {
            hostname: 'localhost',
            port: 8001
        }
    });

For more information on laravel-echo visit https://laravel.com/docs/broadcasting.

Finally

  • Before testing, ensure that you have started the echo-socketcluster-server.

If you experience any challenge, don't hesitate to submit an issue here.