Sep 4, 2014

Chat Application using Java EE 7 - Web Sockets

Use Case - 

Users can join a public Chat room. Users can ask questions and Agent will answer the questions.

High Level Design - 

  • A WebSocketEndPoint which receives & responds to Client (/User) messages. It allows new client to join and maintains list of active users. 
  • Agent is an EnterpriseBean with business logic and answer to all questions. 
  • User will send JSON. Decoders will convert them to JoinMessage or ChatMessage and pass them to WebSocketEndpoint. 
  • EnterpriseBean will call WebSocketEndPoint's sendAll method to send answer to Users. Answer will be a ChatMessage. Encoders will convert the ChatMessage into JSON, before actually passing to Clients. 
  • We have named WebSocketEndPoint class as BotEndPoint, EnterpriseBean as BotBean. 


Design Consideration & Classes -
1. CDI Bean "BotBean" holds business logic and has answer to all question.
2. BotEndPoint (Web socket end point class) allows user to join and receives questions from User. BotEndPoint maintains list of active users. BotEndPoint informs all users if any new user joins, any new question is asked by user, and sends answers to all users.
3. Four different types of Message: ChatMessage, InfoMessage, JoinMessage, and UserMessage. Four type of messages BotEndPoint can send to users. These messages should be converted to JSON, before passing to client. So we have four Encoders to convert one type of message into JSON.
4. Users can send a JoinMessage (to join chat room) or ChatMessage (to ask question). Indeed users will send JSON and Decoders - JoinMessageDecoder, ChatMessageDecoder will convert the JSON into either JoinMessage or ChatMessage correspondingly.
5. BotEndPoint will invoke BotBean to handle the user question. BotBean uses BotEndPoint's sendAll method to send answer to users.
6. User sends a question and BotEndPoint invokes BotBean to prepare answer, and later BotBean invokes BoTEndPoint to send answer to users. But if all this is done in single thread, then user will be blocked for sending second message, until he or she receives respond of first message.
Handling of message by BotBean should be done in separate thread. Indeed we should create pool of BotBean to further imporve performace.

Flow Chart - 

Design - 

More discussion on the design along with code can be found on Oracle site - Java EE 7 tutorial.
http://docs.oracle.com/javaee/7/tutorial/doc/websocket012.htm#BABCDBBC

Share:

No comments

Post a Comment

Comment

© Shift, ShEkUP, Shape, and Surprise | All rights reserved.
Blogger Template Crafted by pipdig