X

Chat Sever using WebSocket in GlassFish 4 (TOTD #207)

Guest Author


A canonical example of using WebSocket is a chat server. This Tip
Of The Day (TOTD) will share the code for
building exactly that!



The complete code in this TOTD is href="https://github.com/arun-gupta/javaee7-samples/tree/master/websocket/chat">available
here.



Here is the code for the Chat Server built as a WebSocket endpoint:


@WebSocketEndpoint("/websocket")
public class ChatEndpoint {
private static final Set<Session> peers = Collections.synchronizedSet(new HashSet<Session>());

@WebSocketOpen
public void onOpen(Session peer) {
peers.add(peer);
}

@WebSocketClose
public void onClose(Session peer) {
peers.remove(peer);
}

@WebSocketMessage
public void message(String message, Session client) throws IOException, EncodeException {
for (Session peer : peers) {
peer.getRemote().sendObject(message);
}
}
}



In this code:
  • Lifecycle callbacks manage the coordinates of the connecting
    clients
  • message method receives all the messages sent by
    the client and then transmits to all the connected clients.


Simple, isn't it ?



You can use a trivial JavaScript to exchange messages between two
browsers. A chat session between two Chrome and Firefox is shown
below:



src="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/Image/437d3d1b40be0639dc17ed5a07a6d147/totd207_chat_output.png">



Download the href="//cdn.app.compendium.com/uploads/user/e7c690e8-6ff9-102a-ac6d-e4aebca50425/f4a5b21d-66fa-4885-92bf-c4e81c06d916/File/58241d0523d2a1cd5f7cbde14dc25b42/totd207_chat.zip">source
code and run it in href="http://dlc.sun.com.edgesuite.net/glassfish/4.0/promoted/glassfish-4.0-b77.zip">GlassFish
4 b77.


Join the discussion

Comments ( 9 )
  • guest Thursday, March 14, 2013

    I am unable to compile the downloaded package totd207-chat.zip: NetBeans complains that javax.websocket.annotations does not exist. According to a NetBeans bug report in January, "jsr250-api.jar is no more in the modules and javax.websocket API jar was missing."

    Obviously this does not tally with recent reports of success - presumably there's some workaround that will let us see the magic in action.

    My config is as follows:

    glassfish-4.0-b80-unix-ml

    NetBeans 7.3 or NetBeans IDE Dev (Build web-main-javaee7-157-on-20130314)

    Java: 1.7.0_17; Java HotSpot(TM) 64-Bit Server VM 23.7-b01

    Runtime: Java(TM) SE Runtime Environment 1.7.0_17-b02

    System: Linux version 2.6.32-45-generic running on amd64


  • Arun Gupta Thursday, March 21, 2013

    APIs have changed in the recent build. Please see the list of changes at:

    https://blogs.oracle.com/arungupta/entry/updating_to_latest_websocket_api


  • guest Thursday, April 11, 2013

    Hello,

    I use Glassfish only for Oracle APEX. I just would like to ask you as you are an expert in Glassfish. Can we rewrite / Redirect the URL in Glassfish ?

    e.g:

    If I type : www.mydomain1.com , I will be redirected to www.mydomain2.com

    OR

    If I type : www.mydomain3.com , I will be redirected to www.mydomain3.com/page1

    Regards,

    Fateh


  • guest Monday, April 22, 2013
  • Ibrahima Tuesday, May 14, 2013

    Hi Arun!

    i follow your tutorial untitled java EE 6 development using Glassfish and Eclipse and in the step of adding database functionality if i try to connect to the sample javaDB database i got this error message :

    Could not connect to A Sample JavaDB Database.

    Error creating SQL Model Connection connection to A Sample JavaDB Database. (Error: org.apache.derby.jdbc.ClientDriver)

    org.apache.derby.jdbc.ClientDriver

    Error creating JDBC Connection connection to A Sample JavaDB Database. (Error: org.apache.derby.jdbc.ClientDriver)

    org.apache.derby.jdbc.ClientDriver

    and i don't now how to resolve it. Please help me to resolve it.

    thaks!


  • Arun Gupta Friday, May 24, 2013

    Ibrahima,

    DId you start the database explicitly, outside the IDE ?


  • Ibrahima Monday, May 27, 2013

    yes i did


  • guest Thursday, August 8, 2013

    Could you please tell me how to send chat to a single user?


  • Arun Gupta Tuesday, August 13, 2013

    That is an application specific logic.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.