Chat Sever using WebSocket in GlassFish 4 (TOTD #207)
By arungupta on Feb 25, 2013
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 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
messagemethod receives all the messages sent by the client and then transmits to all the connected clients.
You can use a trivial JavaScript to exchange messages between two browsers. A chat session between two Chrome and Firefox is shown below:
Download the source code and run it in GlassFish 4 b77.
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
Posted by guest on March 14, 2013 at 03:04 PM PDT #
APIs have changed in the recent build. Please see the list of changes at:
Posted by Arun Gupta on March 21, 2013 at 02:41 AM PDT #
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 : , I will be redirected to
OR
If I type : , I will be redirected to
Regards,
Fateh
Posted by guest on April 11, 2013 at 11:31 AM PDT #
Here are some solutions to URL rewrite:
Posted by guest on April 21, 2013 at 08:14 PM PDT #
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!
Posted by Ibrahima on May 14, 2013 at 07:23 AM PDT #
Ibrahima,
DId you start the database explicitly, outside the IDE ?
Posted by Arun Gupta on May 24, 2013 at 09:43 AM PDT #
yes i did
Posted by Ibrahima on May 27, 2013 at 01:34 AM PDT #
Could you please tell me how to send chat to a single user?
Posted by guest on August 07, 2013 at 11:28 PM PDT #
That is an application specific logic.
Posted by Arun Gupta on August 13, 2013 at 10:17 AM PDT #