Skip to content

Broadcast Configuration

In many modern web applications, WebSockets are used to implement realtime, live-updating user interfaces. When some data is updated on the server, a message is typically sent over a WebSocket connection to be handled by the client. This provides a more robust, efficient alternative to continually polling your application for changes.

Trademen supports several broadcast drivers out of the box:

  1. Pusher
  2. Redis

Pusher Driver Ingratiation

Update your broadcast driver to pusher in your .env file:

BROADCAST_DRIVER=pusher

After updating .env file. Clear your config file by following command:

php artisan clear:all

Sign Up an account if you don’t have any. After logging in your account create a new app for your exchange. Once creation is done you will be redirect to a page where app keys tab will have all the credential you need. Copy them and paste them under the related keys in .env file. check

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

After adding these variables run the following command:

npm run production

If you setup all correctly then you have configured broadcast successfully.

Redis Driver Configuration

The Redis broadcaster will broadcast messages using Redis' pub / sub feature; however, you will need to pair this with a WebSocket server that can receive the messages from Redis and broadcast them to your WebSocket channels.

Installing Laravel Echo Server

To install Laravel Echo server you need to run the following command:

sudo npm install -g laravel-echo-server

Run the init command in your project directory. It will ask you several questions to configure of your server.

laravel-echo-server init

The cli tool will help you setup a laravel-echo-server.json file in the root directory of your project. For more details: Laravel Echo Server

You may edit this file later on to manage the configuration of your server.

To manage the configuration open laravel-echo-server.json file with your text editor

sudo vim laravel-echo-server.json

Now you have to update your broadcast driver to redis in your .env file:

BROADCAST_DRIVER=redis

After adding these variables run the following command:

npm run production

Configuring Echo Server

Supervisor configuration files are typically stored in the /etc/supervisor/conf.d directory. Within this directory, you may create any number of configuration files that instruct supervisor how your processes should be monitored. For example, let's create a echo-server.conf file that starts and monitors a echo-server process:

[program:echo-server]
process_name=%(program_name)s
command=laravel-echo-server start --dir=/var/www/html
autostart=true
autorestart=true
user=root
redirect_stderr=true
stdout_logfile=/var/log/echo-server.log

If your server does not have root user, then change the user as well. Make sure /var/log/echo-server.log the file is created and has write permission

Starting Supervisor

Once the configuration file has been created, you may update the Supervisor configuration and start the processes using the following commands:

supervisorctl reread

supervisorctl update

supervisorctl start echo-server

You need to run the following command to check if your supervisor is running successfully.

supervisorctl status