Sessions

It is quite likely that, occasionally, a chat client will simply disappear without formally notifying the server of its intention to leave the chat room. This can occur for a number of reasons, such as a system crash, loss of network connectivity, programming error, or a malicious attempt to waste resources in the server. For example, the chat server must accumulate state for each pull client until the client polls for the next batch of updates. If a client terminates unexpectedly, the server will continue to accumulate chat updates forever and eventually run out of memory unless we take precautions.

To address this issue, the chat application uses the notion of a session to handle the chores of authorization and resource reclamation. A session is an Ice object that is implemented by the chat server and acts as the client's gateway to the chat room. In order to join the chat, a client must first request a new session. In return, the client receives a proxy for its session object; all of the client's interactions with the chat room and its participants occur via this proxy. The session also serves as a repository for resources that the server allocates on behalf of the client. The server can easily reclaim those resources when the session is destroyed.

A push client, which is normally waiting passively for callback notifications, is responsible for keeping the session alive by either sending harmless ping requests at regular intervals or configuring active connection management to enable "heartbeat" messages on the connection. That is not necessary for a pull client because the client's polling activity keeps the session active, provided the poll interval is shorter than the session's expiration timeout. A well-behaved client will also destroy the session when it is no longer needed; if the client neglects to destroy its session, the server will automatically destroy it after a period of inactivity. The diagram below depicts a push client's interaction with the chat server and its session.
 


As you can see, sessions relieve the server from any concerns about resource exhaustion due to faulty clients or networks. Furthermore, sessions are simple to implement and have low run-time overhead, making them an ideal choice for effectively managing resources in a distributed environment.