Search code examples
rubynetwork-programmingserver-side

How to manage multiple client sessions within server application?


I'm writing web chat application, similar to GTalk. It based on Orbited + Sinatra for client side, and Ruby for server side. I've already implemented all the protocol, everything working good.

But, I don't know how to deal if there are multiple connections from one user. Let's say for example, I logged to chat from 2 different browsers. Google handles that really nice, two chats appear to be exactly the same. But my app just shows 2 exactly the same users in contact list, which is incorrect.

Here is a small example of server clients pool:

Server
--> Connections
      |
      - Client (User Information, ConnectionID)
      - Client (User Information, ConnectionID)
      ....
      - Client (...)

I have 2 types of messages: Private (user-to-user), Public (user-to-conference).

I'm trying to figure out how to deal with such situation? Any suggestions?


Solution

  • Sorry for the vague answer, but here goes: you need to "push" chat text out to every connection for a given user ID, not just responde to a "pull" from a given connection / session.

    I don't know how your client works, but if it polls for updates, you probably need to save a per user account image of recent messages in a database, then get all the relevant updates for that user from the DB, and not just associate the chatting with "point to point" sessions.

    Grr. I don't have time to explain this better now...

          • update: - - - - -

    Make some kind of "set" data structure for each conversation identifying the sessions (and therefore users) involved, regardless of whether it is one on one, or a large group. Make a list of posts for each conversation, ordered chronologically, which you can scan to update the display of each client, supplying any as yet un-viewed posts.

    As an aside to a comment on the question itself: Somebody made the point that "it's been done", download the code. Perhaps that's a valid point, perhaps not. If you can find an existing code base in a form you can embrace and extend, great. If not (because it's homework, or because corporate policy says "do it from scratch, here and now", well, then, downloading a "solution" is not a valid criticism, is it?