This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Help with Chat Program, update user list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with Chat Program, update user list" Watch "Help with Chat Program, update user list" New topic
Author

Help with Chat Program, update user list

James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

I have a chat program that I made. It has a ListModel on teh client side that displays the users that are currently logged into the system. Once the server authenticates the login it sends that username to all clients so it can be added to the ListModel. The problem with this approach is the new clients do not see the users that have already logged in since those usernames have already been sent out. These users are added to an ArrayList on the server so I am thinking I can just send the ArrayList instead of the one username and then update the ListModel on the clients. Or should the server loop through the ArrayList and send a delimited String with all the users? So I guess my question is do I let the client loop through the ArrayList or should I let the Server do it? The code is kinda long so I will post is only if someone really wants to see it. This is more a question of design than coding.

Thanks for the opinions!


Visit my blog! http://jameshambrick.com
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Sending the entire user list each time seems inefficient. To me it seems like you want two kinds of messages:
- when someone logs in, that user gets a list of all other logged in users; this seems to be the only message you're missing
- when someone logs in, all other users get only the newly logged in users

This does mean you'll get data replication, since both the server and all clients have a full list of users, but the alternative is retrieving the full user list each time any client needs it.
On the other hand, that alternative does mean that there is only one copy of the list, so there won't be any problems with synchronizing the lists. If a new-user-message fails to arrive at a client, that client won't have the full list. That may not be that bad, but missing a logout message makes the client believe the user is still logged in. Ouch.

So, you'll have to decide:
1) keep a full copy of the user list both on the server and on the clients. Pro: less network traffic. Con: synchronization needs to be handled properly.
2) keep a copy only on the server and send it to the clients upon request. Pro: no need to synchronize, an error means no user list. Con: more network traffic.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Thanks for the advice!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

You're welcome.
Werner Fletcher
Ranch Hand

Joined: Apr 19, 2011
Posts: 34
Hi there James! Did you manage to get this working? I'm struggling with the exact same issue! Do you mind telling me what you did please? I'm really desperate for help and this seems to be the only forum with a solution. Thanks!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
Welcome to the Ranch

Shall I contact James Hambrick and ask him to have another look at this thread?
Werner Fletcher
Ranch Hand

Joined: Apr 19, 2011
Posts: 34
That would be amazing! Thanks for the warm welcome
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37874
    
  22
You're welcome
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Werner Fletcher wrote:Hi there James! Did you manage to get this working? I'm struggling with the exact same issue! Do you mind telling me what you did please? I'm really desperate for help and this seems to be the only forum with a solution. Thanks!


I did get that part of the program working.

When a new user logs in they are added to an arraylist called userArrayList
To send this user list to the new users that have just logged in I made a loop and turned the list into a comma delimited string.
Then the client splits the string and puts it into an array. Then adds the users to the listModel.
The currently logged in users just get a message string that has the new user's name so it can be added to their listModel.

Hope this helps. If you have anymore specific questions just let me know.
Werner Fletcher
Ranch Hand

Joined: Apr 19, 2011
Posts: 34
James Hambrick wrote:
I did get that part of the program working.

When a new user logs in they are added to an arraylist called userArrayList
To send this user list to the new users that have just logged in I made a loop and turned the list into a comma delimited string.
Then the client splits the string and puts it into an array. Then adds the users to the listModel.
The currently logged in users just get a message string that has the new user's name so it can be added to their listModel.

Hope this helps. If you have anymore specific questions just let me know.


Hey thanks for replying!
ArrayList!!! You're a freaking genius! All this time I am using a HashTable and it's been tricky to work with. The only thing that I am battling with now is that I need to check the list for duplicates... any idea that would make it easier? I am working on it in the meantime... Thanks again!
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

Werner Fletcher wrote:
James Hambrick wrote:
I did get that part of the program working.

When a new user logs in they are added to an arraylist called userArrayList
To send this user list to the new users that have just logged in I made a loop and turned the list into a comma delimited string.
Then the client splits the string and puts it into an array. Then adds the users to the listModel.
The currently logged in users just get a message string that has the new user's name so it can be added to their listModel.

Hope this helps. If you have anymore specific questions just let me know.


Hey thanks for replying!
ArrayList!!! You're a freaking genius! All this time I am using a HashTable and it's been tricky to work with. The only thing that I am battling with now is that I need to check the list for duplicates... any idea that would make it easier? I am working on it in the meantime... Thanks again!



I have not worked on ensuring that only one person can login with the same username at a time. But with the ArrayList of users on the server it would be as simple as going through the ArrayList and searching for that username. If the username is in the list then send a msg back to the client. This is the next part that I will be working on when I get time at work.
Werner Fletcher
Ranch Hand

Joined: Apr 19, 2011
Posts: 34
Great! My list is working properly! I have even made a thread that updates it every second. Now I have another challenge ahead of me.... deleting users when they quit. I'm enjoying making this chat program alot! Thanks again for your help.
James Hambrick
Ranch Hand

Joined: Sep 04, 2004
Posts: 282

With my code the server gets a message when the client disconnects so that particular user is taken off the list then. I also add users to the list when they are authenticated by the server. So with my code there is no need to have a thread updating the arraylist on a set interval.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with Chat Program, update user list
 
Similar Threads
conference chat app
URGENT!!!
Chat Application 2
Java chat application implementation method
Chat application implementation method?