brti
926
22
Laravel

Broadcasting Event to Specific Client

Posted 4 years ago by brti

What's an easy way of broadcasting an event to a certain client, or a group of clients with socketio, and nodeJS? I have tried to get the Laravel session ID from the cookie with nodeJS but was not successful in doin so.

Here's the code in my socket.js file that handles broadcasting the data to the channel.

redis.psubscribe('*', function(error, count) {});

redis.on('pmessage', function(subscribed, channel, message) {
    console.log(message);

    message = JSON.parse(message);

    io.emit(channel + ':' + message.event, message.data);
});

One way I thought of doing it was by passing through an array of session IDs to the event object and then checking in the socket.js file to see if the current client has a matching session ID. It would probably end up looking something like this:

socket.js file:

message.data.receivers.forEach(receiver) {
    if (receiver == sessionID) {
        io.emit(channel + ':' + message.event, message.data);
    }
}

However this would rely on having to get the session ID from the cookie sent in the request via nodeJS, which I have not been able to do.

Any suggestions?

Please sign in or create an account to participate in this conversation.