Broadcast event with Socket.io and Redis in Laravel 5.3

I am trying to create a simple event translation option with L5.3 using socket.io and Redis. Everything works fine on the server side, but does not work on the client side. here is my code.

Event code

public $data; public function __construct($data) { // $this->data = $data; } /** * Get the channels the event should broadcast on. * * @return Channel|array */ public function broadcastOn() { return new PrivateChannel('test-channel'); } 

Socket.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(); redis.subscribe('test-channel', function(err, count) { }); redis.on('message', function(channel, message) { console.log('Message Recieved: ' + message); message = JSON.parse(message); io.emit(channel + ':' + message.event, message.data); }); http.listen(3000, function(){ console.log('Listening on Port 3000'); }); 

View file

 <script type="text/javascript"> var socket = io('http://localhost:3000'); socket.on('test-channel: App\\Events\\SomeEvent', function(message) { alert("not yet reached here") jQuery('#dynamic_content').append(message.data.name); }); </script> 

fire alarm code

 Route::get('test', function () { // Route logic... $data = array("id" => "1","name"=>"jobin" , "amount" => "1000"); Redis::publish('test-channel', json_encode($data)); event(new App\Events\SomeEvent($data)); return "event fired"; }); 

when I am domain.app/test , the node server console already contributed the result as shown below.

 Message Recieved: {"id":"1","name":"jobin","amount":"1000"} 

I already tried several threads, I still can not understand what I am missing here.

My env

 BROADCAST_DRIVER=redis CACHE_DRIVER=redis SESSION_DRIVER=database QUEUE_DRIVER=redis 

only the problem cannot reflect it on the client side, the node server console and the EventListener descriptor method work fine (I can dump the data there).

Any ideas guys last 6 hours working on this.

already tried these any hint would be much appreciated. Thanks.

+1
source share
1 answer

Private channels add private_ in front of the name. In this case, the channel name will be private_test-channel

+2
source

Source: https://habr.com/ru/post/1011955/


All Articles