I have a small Laravel Echo Server setup using this package (https://github.com/tlaverdure/laravel-echo-server). I have the following setup:
// app.js
import Echo from "laravel-echo";
window.Echo = new Echo({
broadcaster: 'socket.io',
host: 'http://vaprobash.dev:6001'
});
window.Echo.channel('documents-sharing')
.listen("Api\\DocumentShared", (event) => {
console.log(event);
});
.
// server.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
http.listen(3000, function(){
console.log('Listening on Port 3000');
});
redis.subscribe('documents-sharing', function(err, count) {
console.log("subscribed " + count);
});
redis.on('message', function(channel, message) {
message = JSON.parse(message);
console.log('Message Received: ', channel, message);
console.log(io.to(channel).emit(message.event, message.data));
});
.
// laravel-echo-server.json
{
"authHost": "http://localhost",
"clients": [
{
"appId": "986bb8954bb1e2e9",
"key": "58ca6a205718590ad5fd88a1afc1877c"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port": "6379",
"host": "localhost"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "localhost",
"port": "6001",
"protocol": "http",
"socketio": {},
"sslCertPath": "",
"sslKeyPath": ""
}
.
// DocumentShared.php
class DocumentShared implements ShouldBroadcast, ShouldBroadcastNow
{
use SerializesModels, InteractsWithSockets;
/**
* @var User
*/
public $user;
/**
* @var Resource
*/
public $resource;
/**
* @var Share
*/
public $share;
/**
* Create a new event instance.
*
* @param User $user
* @param Resource $resource
* @param Share $share
*/
public function __construct(User $user, Resource $resource, Share $share)
{
$this->user = $user;
$this->resource = $resource;
$this->share = $share;
}
public function broadcastOn()
{
return new Channel("documents-sharing");
}
}
.
// app.blade.php
<script src="{{ asset('js/socket.io.min.js') }}"></script>
<script src="{{ asset('js/home.js') }}"></script>
After that I run:
gulp watch
redis-server
node server.js
laravel-echo-server start
php artisan queue:listen
When the /notify
route is hit, I fire the event.
Route::get('notify', function() {
$user = User::first();
$resource = Resource::first();
$share = Share::first();
event(new DocumentShared($user, $resource, $share));
return 'done';
});
I can see the server has received the event and it didn't show on the queue.
The problem is that the client side couldn't connect to the server because the port wasn't responding. Ironically, I just removed the host
key from the laravel-echo-server.json
config file and re-run the server. Everything worked like a charm!