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


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
  • 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:



Download the source code and run it in GlassFish 4 b77.

Comments:

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:

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

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 : 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

Posted by guest on April 11, 2013 at 11:31 AM PDT #

Here are some solutions to URL rewrite:

http://stackoverflow.com/questions/3216973/urlrewritefilter-with-glassfish

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 #

Post a Comment:
Comments are closed for this entry.
About

profile image
Arun Gupta is a technology enthusiast, a passionate runner, author, and a community guy who works for Oracle Corp.


Java EE 7 Samples

Stay Connected

Search

Archives
« May 2015
SunMonTueWedThuFriSat
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
      
Today