laravelsocketsnode-redisweb-notificationslaravel-broadcast

Laravel with Socket/Redis - Private channel routes not working


I am kinda stuck in broadcast routes. i setup a socket server with redis and configured it with Laravel. For public channel ,everything is working fine but when it comes to private or presence channel, it is somehow bypassing laravel broadcast routes. Can't figured out how & why.

i have attached a repo link so you guys can explore it too. Plus some quick bits are also below.

https://github.com/bilahdsid/socket-laravel/tree/socket

TestEvent.php

class TestEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $data;

    public function __construct()
    {
        $this->data = array(
            'power'=> '10'
        );
    }

    public function broadcastOn()
    {
        return new PrivateChannel('test-channel1');
    }

    public function broadcastWith()
    {
        return $this->data;
    }
}

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();


redis.subscribe('private-test-channel1', function(err, count) {

  console.log(err);
});

redis.on('connection',function (socket,channel) {

  console.log(socket+''|+channel);
});

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');
});

io.on('connection', function(socket){
  console.log('a user connected');
});

routes/web-- for firing

Route::get('/', function () {
    return view('home');
});

Route::get('fire', function () {
    // this fires the event
    broadcast(new App\Events\TestEvent());
    return "event fired";
});

routes/channel.php -- below line doesn't work-- main issue

Broadcast::channel('private-test-channel', function ($user, $id) {

    echo '1111'; exit;
    return (int) $user->id === (int) $id;
});

Thanks.


Solution

  • As far as I can see you are defining a channel with the name: test-channel1:

    public function broadcastOn()
    {
        return new PrivateChannel('test-channel1');
    }
    

    but in routes/channels.php:

    Broadcast::channel('private-test-channel', function ($user, $id) {
    

    Sounds like a typo!