views:

244

answers:

2

Hello folks,

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 got a problem - dont 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).

Im trying to figure out how to deal with such situation? Any suggestions?

A: 

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?

Roboprog
Ok, im already using server push approach. The thing is - when you log in with your account into the chat from different browser tabs - it creates another connection on server with the same user, so there are 2 or more same users on chat list. I cant figure out the best way to allocate session pool for users who have more than one connection.
Dan Sosedoff
A: 

When your clients connect, you should give them a unique identifier. Classic ASP had that SessionID but you just need something unique, maybe sending that key back to client through a cookie.

After that, any message sent by clients should be placed in a common area; again in classic ASP you had that Application objects, nicely suitable for this task.

From this, you can go anywhere: implement chat rooms, filtering messages, whispering something, etc.

I did something like that about 7, 8 years ago, storing messages in XML files. But you could also use some database to do that.

As pseudo code, you'll have following for every web request:

    if request DON'T have cookie ID
       create an unique ID and set cookie

    process incoming action
       case "private":
            write message for that unique id
       case "public"
            write message without target user
       case ...

    display user interface
        list all messages for your unique id, or without target
        create a users list, using unique IDs as value (except yours)

    refresh every n seconds

Rubens Farias
No no, i got the point. But in your case if you login to your chat app from different locations with the same account you will receive messages from yourself. Right? Or i just dont see the idea of keeping uniq id for each connection.
Dan Sosedoff
you'll not receive messages from yourself, as second connection will have another unique id.
Rubens Farias