Today we will install and configure the combination of Laravel Echo Server and Socket.IO.
Laravel Echo Server is the server-side part of the Laravel Echo package, which allows establishing a connection between the client and server side, enabling real-time message exchange.
Socket.IO is a client-side library for working with Socket.IO, which provides the ability to establish a two-way connection between the client and server in real-time. Socket.IO is a popular module for working with web sockets and provides support for instant two-way communication between the client and server.
The installation will be carried out on Laravel 10 and Node.js v12.22.12.
npm install -g laravel-echo-server
laravel-echo-server init
npm install --save laravel-echo socket.io-client
import Echo from 'laravel-echo';
import io from 'socket.io-client';
window.io = io;
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
encrypted: false,
transports: ['websocket'],
});
npm run prod
.
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 TopServiceProgressUpdated implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $topServiseProgress;
/
* Create a new event instance.
*
* @return void
*/
public function __construct($topServiseProgress)
{
$this->topServiseProgress = $topServiseProgress;
}
/
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('top-service-progress');
}
}
TopServiceProgressUpdated::dispatch($data);
window.Echo.channel('top-service-progress')
.listen('TopServiceProgressUpdated', (event) => {
console.log('Progress task: ', event.topServiseProgress.original);
});
window.Echo.connector.socket.on('connect', () => {
console.log('Socket connected');
});
window.Echo.connector.socket.on('disconnect', (reason) => {
console.log('Socket disconnected:', reason);
});
window.Echo.connector.socket.on('connect_error', (error) => {
console.error('Connection error:', error);
});
If you receive laravel-echo-server: command not found when entering the command laravel-echo-server init
, you should follow these steps:
npm list -g --depth=0
npm install -g laravel-echo-server
echo $PATH
export PATH=$PATH:$(npm config get prefix)/bin
source ~/.bashrc
laravel-echo-server init
In my case, I encountered the following error:
Connection error: Error: It seems you are trying to reach a Socket.IO server in v2.x with a v3.x client, but they are not compatible (more information here: https://socket.io/docs/v3/migrating-from-2-x-to-3-0/)
I had to revert to an older version of socket.io using the following command:
npm install socket.io-client@2.3.0 --save