You would need to implement a publisher subscriber model.Basically have each of your users implement an interface
interface chatWithUser {
public String getMessage();
public List<ChatWithUser> getFriends();
}
All these users are subscribers to the chat feature. Now we will have a publisher which would have a list of subscribers. Now when a request for chat comes from a users socket the publisher will check the list of users
public class PublishChattMessage
{
List<chatWithUser> userChatList;
public void setUpChat(ChatUser mainUser )
{
for(ChatWithUser user:userChatList)
{
List<ChatUser> userList = user.getFriends()
// set up connections with all friends
}
}
/**
* Method to chat between two users. u
**/
public void chat(ChatUser userOne,ChatUser user2)
{
}
}
Once the connection is set up with all friends then call the chat method and display the messages