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.
views:
333answers:
4I 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:
- Need a table with a list of users and passwords for authentication
- Need a table for the currently logged in uses
A. needs a time stamp field of the last contact - When a users does something update the last contact field to the current time
- If the user' last contact time is > current time + 2 minutes then they are logged out
- client side application will need to send periodic messages to the server to say "Im still here"
- 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.
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:
- Client calls web service 'handshake' and provides some minimal identification details.
- Server returns an acknowledgment that includes a unique client identifier.
- Client calls web service 'message' and sends a new message, together with its identifier.
- 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-)
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.
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
- ...