Role-based chat system in Nodejs

297 views Asked by At

I've implemented a chat feature using socket.io and nodejs. But now I want to implement a role based chat application. For example, if someone wants to communicate with someone who is in a higher position, first needs to send a request. I've implemented a chat request system(similar to friend request). But I dont know how to make it role-based. Can accesscontrol package be helpful in this case?

I've added a role property into the user model. I wanted to create a middleware to check what role has the user and to whom it wants to send the message. if its allowed it can continue with the 'chat', but if its not allowed I want it to redirect it to the request endpoint. I wanted to know if this logic is ok. If not, what could I do better.

EDIT: This solution with the middleware it seems to work :). If you have any better idea please answer my question

1

There are 1 answers

0
Eliav Cohen On

I don't have enough information to know exactly your case but it doesn't sound like you need access control. Here's my suggestion for you:

  • you need to store a role for each user.
  • when someone sends a message for the first time, you check if they can do it, doing something similar to this:

const senderRole = 'normal', receiverRole = 'extreme'; // YOU NEED TO FETCH THESE FROM YOUR DB

const roles = ['normal', 'better', 'extreme', 'best'];

if (roles.indexOf(senderRole) < roles.indexOf(receiverRole)) {
  // need to ask for request
}

  • when they need to ask for request you would open a chat with the receiver but set in your database a special setting on the chat model: (access=false)

  • when the receiver goes in the app, they have a special section for chats filtered with the option (access=false), when they approve the chat, you change the setting and enable the chat to go back into the main queue and display messages