Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

using socket how to broadcast message to specific users who has role_id = 1

Using Socket and node express i want to send a
message to all users who has role_id 1
but whenever i broadcast message all users receive it.

// let suppose this is message
let Message = 'hello all users'
//socket code
socket.emit('message', Message);

//but i want to send message to below users.
// I am not a good programmer and i am new to socket.



//during user login time i return this 
if (login == true) {
  user.status = 'Online';
}

status has nothing with message.
its just updating user status

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :


//first of all join user to room if has role id 1
//during user login time i return this 
let role_room_id = 1;
if (login == true) {
  user.status = 'Online';
  socket.join(role_room_id);
}

///then emit message in that room
socket.broadcast.to(role_room_id).emit('message', Message);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading