I've been searching for almost 10 hours and just found similar question, that hasn't been answered they way it should be though I've tried my best to make it possible. Sorry if it is just dumb question but either I'm not aware or it's not working the way it should be.
So my angular 2 component has something like
import * as io from "socket.io-client";
export class TasklistComponent implements OnInit {
ngOnInit() {
var socket = io("http://localhost:3000");
socket.on("test-channel:TasklistModified", function(message) {
console.log(message);
});
}
}
I've configured socket.js
on my laravel root directory.
var app = require('express');
var http = require('http').Server(app);
var io = require("socket.io")(http);
var Redis = require("ioredis");
var redis = new Redis({
scheme: 'localhost',
port: 6379, // Redis port
host: '127.0.0.1'
});
redis.subscribe('test-channel', function(err, count) {
//..
});
redis.on('message', function(channel, message) {
console.log("Message Recieved", channel, message);
message = JSON.Parse(message);
io.emit(channel + ":" + message.event, message.payload);
});
http.listen(3000, function() {
console.log('listening on port 3000');
});
And I've the following event class and route on laravel routes.php
Route::get('event', function() {
event(new TasklistModified([
'name' => 'test'
]));
});
//Event Class
use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class TasklistModified extends Event implements ShouldBroadcast
{
use SerializesModels;
public $tasklist;
public function __construct($tasklist)
{
$this->tasklist = $tasklist;
}
public function broadcastOn()
{
return ['test-channel'];
}
}
I've open two browser and tried running,
Angular : localhost:4200
And Laravel
localhost:8000/event
I couldn't get any response when I hit event
route in laravel.
Applications Running
Node: 3000
Laravel: 8000
Angular2: 4200
Your answers will be appreciated a lot. Thanks