views:

333

answers:

4

I have a school project in which I have to implement a chat application, who's server will be a java web service.
The problem is that I've always thought of a web service as a way of calling remote functions, and I have no idea how to keep a "session" active on the web service, nor how to keep track of all the people currently in chat, rooms etc.

A point in the right direction would be appreciated.

A: 

I don't know Java so this answer will be language agnostic.

In my opinion the simplest way to do this without running a process on the server would be to store all your data in a database.

Here is a short list of the basic things that will need to be done:

  1. Need a table with a list of users and passwords for authentication
  2. Need a table for the currently logged in uses
    A. needs a time stamp field of the last contact
  3. When a users does something update the last contact field to the current time
  4. If the user' last contact time is > current time + 2 minutes then they are logged out
  5. client side application will need to send periodic messages to the server to say "Im still here"
  6. You'll need to find a way to determine when a message has been sent and when to update the client's display that a message has been received, this I will leave to you.

If you still need some help here is an AJAX/ASP.Net chat app that should (I didn't look at its source) work much the same way.

Unkwntech
You forgot the link to the app...
RedFilter
+1  A: 

To the best of my knowledge, a chat server is supposed to know its clients after an initial connection, and send every client message to all clients. This definitely calls for some sort of session maintenance. I think the right way to do this is as follows:

  1. Client calls web service 'handshake' and provides some minimal identification details.
  2. Server returns an acknowledgment that includes a unique client identifier.
  3. Client calls web service 'message' and sends a new message, together with its identifier.
  4. Server identifies client by the identifier, distributes message to all clients.

I'm not really sure how the message distribution should work, as web services are essentially a pull-service and not push. Perhaps the client should expose its own web service for the server to call.

Hope this helps,

Yuval =8-)

Yuval
the 'clients' will likely need to pull new messages, which can be done in same method as the 'heartbeat' that I sugested.
Unkwntech
That could work too... although maintaining the status of every message is costly. The server could send the entire message log each time, but that would be costly to the client (not to mention clog the network).
Yuval
That's almost what we eventually did. 3 major calls to the webservice by the client - register (submit my info), send a message, and pull all messages waiting for me.We saved the messages as hash-table of linked lists, where the key of the hash-table is the user for whom the messages are waiting, and the value is a list of messages.The main lesson from this project was is that the WebService technology is most inappropriate for this kind of thing.Thanx for all the replies.
MeLight
A: 

I wrote a chat engine which had a service in the background and everything stored in a database, an input form frame and an output frame which received the html stream.

If you want to skip the service part and only implement via a web service, you need to implement at least two operations: Post for inputs, and GetLatestChanges to receive the chat's output, which translates into HTML using some Javascript magic.

Of course you need to keep track of rooms, users, messages, which user receives which texts etc, as sketched by Unknwntech.

devio
A: 

You could consider implementing a COMET solution. This will effectively give you push communication, thus eliminating latency, a VERY nice feature for a chat application.

If you want to go for the gold, consider implementing more advanced features:

  • spell check
  • URLs/email addresses converted to links automatically
  • separate chat rooms
  • moderator functions (terminate chat, kick user)
  • event info like "User is typing..."
  • statuses (available, busy, away...)
  • avatars
  • ...
RedFilter