'redis' => [
'client' => 'predis',
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DATABASE', 0),
],
],
'default' => env('BROADCAST_DRIVER', 'redis'),
BROADCAST_DRIVER=redis
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class ActionEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct()
{
//
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class ActionEvent implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $actionId;
public $actionData;
/**
* Create a new event instance.
*
* @author Author
*
* @param mixed $actionId
* @param mixed $actionData
* @return void
*/
public function __construct($actionId, $actionData)
{
$this->actionId = $actionId;
$this->actionData = $actionData;
}
/**
* Get the channels the event should broadcast on.
*
* @author Author
*
* @return Channel|array
*/
public function broadcastOn()
{
return new Channel('action-channel-one');
}
/**
* Get the data to broadcast.
*
* @author Author
*
* @return array
*/
public function broadcastWith()
{
return [
'actionId' => $this->actionId,
'actionData' => $this->actionData,
];
}
}
event(new ActionEvent($actionId, $actionData));
$actionId = 'score_update';
$actionData = array('team1_score' => 46);
event(new ActionEvent($actionId, $actionData));
npm install Redis-server
npm install express ioredis socket.io dotenv –save
'use strict';
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
require('dotenv').config();
var redisPort = process.env.REDIS_PORT;
var redisHost = process.env.REDIS_HOST;
var ioRedis = require('ioredis');
var redis = new ioRedis(redisPort, redisHost);
redis.subscribe('action-channel-one');
redis.on('message', function (channel, message) {
message = JSON.parse(message);
io.emit(channel + ':' + message.event, message.data);
});
var broadcastPort = process.env.BROADCAST_PORT;
server.listen(broadcastPort, function () {
console.log('Socket server is running.');
});
<div class="container">
<h1>Team A Score</h1>
<div id="team1_score"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.2.0/socket.io.js"></script>
<script>
var sock = io("{{ env('PUBLISHER_URL') }}:{{ env('BROADCAST_PORT') }}");
sock.on('action-channel-one:App\\Events\\ActionEvent', function (data) {
// data.actionId and data.actionData hold the data that was broadcast
// process the data, add needed functionality here
var action = data.actionId;
var actionData = data.actionData;
if (action == "score_update" && actionData.team1_score) {
$("#team1_score").html(actionData.team1_score);
}
});
</script>
Conclusion
I hope it will help you make real-time applications like chatbots and games with live score updates and broadcast some notice to all users. Regarding Laravel broadcasting, there are other methods for receiving broadcasts. You can also use the JavaScript library Laravel Echo to subscribe to channels and receive messages from the broadcast.
At ZealousWeb, our recent project includes the implementation of this robust functionality. We used real-time Laravel broadcasting to build a game that involves participants and judges from across the world. It is a scheduled game that needs active participation from pre-listed candidates and works in real time. If you’re seeking experienced Laravel developers to implement Laravel broadcasting in your projects, our Laravel development company has the needed expertise. For more information or to get in touch with our team, feel free to contact us. We’re here to help bring your projects to life with our specialized skills and innovative solutions.
Does that sound exciting to you? It sure was for us to create the game!
You can always get in touch to learn more about how we created certain functionalities or ask us, a Laravel development company, to create it for you! Alternatively, you can hire a Laravel developer from us who’ll work only on your project.