Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davorminchorov's avatar

How To Send Notifications To A Specific User Group / Role Only using Socket.io?

Hello!

I am working on a project which I want to implement a feature where after an action happens, I want to notify all online administrators on the web app and show them a message that an action happened. Only the users with the role of admin can see these notifications

I have a few questions:

  1. How can I identify which user is online and has an admin role? I am looking for some ideas around "show all online users" list

  2. Should I put these admins into a room where I can broadcast the notification only to that room?

  3. How can I get all users who are online and associate their socket ID with the ID / Username in the database?

I will also need the online status to be shown in a table so I can know if UserOne and John are online at the moment.

  1. How can I keep track of the online / offline status? Do I have to keep the status somewhere outside of the database? I am using Laravel 5.1, NodeJS 5.0, Socket.io version 1.3 and Redis to play around with the notifications.

Does anyone have any idea how can I deal with this problem? I am not really looking for any code specific stuff, I am just curious about the theory behind the idea and how it will work.

Thanks!

0 likes
7 replies
davorminchorov's avatar

@Goranata Yup, but that's not it. I am looking for a way on the NodeJS end to work with the notifications. The Laravel part is not the problem.

I've made some progress since I've asked this question, now playing around with Redis and saving / seearching / getting online users.

My current problem is if the user has multiple socket IDs (or open another window in the same browser with other words).

Cheza's avatar

@Ruffles May you please explain how you managed to achieve that? I'm in a similar quagmire myself at the moment.

davorminchorov's avatar

@Cheza I managed to find a way to implement the whole feature (it's almost done) and I might write a guide soon.

What exactly do you need help with?

1 like
veve286's avatar

@Ruffles I am also interesting. Please write tutorial for that. I want to know how you restrict the channel.

1 like
aixguru's avatar

One way would be to use something like JWT. Include user roles (or any other details you like) as part of the payload. Then have your node server run a check to see if the user has permission to join the room.

Something like

    socket.on('subscribe', function (room) {
        if ( token.role == 'admin')
        {
            socket.join(room);
        }
        else
        {
            console.error('Not authorized');
        }
    });

This guide helped me get it all set up my first time.

Please or to participate in this conversation.