laravel broadcast with socket.io

64 Views Asked by At

In laravel I have PriceBroadcast:

class PriceBroadcast implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $prices;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($prices)
    {
        $this->prices = ($prices);
        $this->socket = "price.updated";

    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function broadcastOn()
    {
        return new Channel('price');
    }
    public function broadcastAs()
    {
        return "price.updated";
    }
}

and I want to use socket.io . I have server.js:

const { Server } = require('socket.io');
const { createClient } = require('redis');
const { createAdapter } = require('@socket.io/redis-adapter');
const io = new Server();
const pubClient = createClient({ socket:{host: host,port: port},password:password });
const subClient = pubClient.duplicate();

io.adapter(createAdapter(pubClient, subClient));
io.on('connection', function (socket) {
    console.log("connection")
});
io.on("price.updated",function (s,m){
    console.log(s)
    console.log(m)
})
io.listen(3000);

I'm using postman to test this. when I connect I get "connection" in console. But when PriceBroadcast is called, there is no log in console.

I tried this to:

subClient.on('subscribe', (channel, count) => {
    console.log(`Subscribed to ${channel}. Count: ${count}`);
});

but there is no log. And with redisInsight I can see pricebroadcast in redis.

0

There are 0 best solutions below