@WebSocketClient in GlassFish 4 (TOTD #204)

Tyrus is the Reference Implementation for Java API for WebSocket (JSR 356) and is already integrated in GlassFish 4. The implementation is rapidly evolving and and adding support for different features already defined in the latest version of the specification.

The latest integration in GlassFish added support for creating Java-based WebSocket clients.

Any POJO can be made a WebSocket client by adding @WebSocketClient. A inbound message is received in a method annotated with @WebSocketMessage. Any method can be designated as lifecycle method by specifying @WebSocketOpen, @WebSocketClose, @WebSocketError. Here is a simple sample:

public class MyClient {
public void onOpen(Session session) {
System.out.println("Connected to endpoint: " + session.getRemote());
try {
} catch (IOException ex) {
Logger.getLogger(MyClient.class.getName()).log(Level.SEVERE, null, ex);

public void processMessage(String message, Session session) {
System.out.println(getClass().getName() + ": " + message);

In this code, a message is sent to the connected WebSocket endpoint whenever the connection is initiated. An inbound message (coming from the server) is processed using processMessage method.

The endpoint code is pretty trivial:

@WebSocketEndpoint(value="/websocket", configuration=DefaultServerConfiguration.class)
public class MyEndpoint {

public String sayHello(String name) {
System.out.println(getClass().getName() + ": " + name);
return "Hello " + name;

The endpoint has a single method that receives the payload, appends a string and returns back as the response. Even though server is returning the response right away, the client views this as two separate messages, one outbound and the other one inbound. So any message-exchange-pattern such as request/response need to be specifically built using the application code. The configuration attribute, in this case, is optional and will be fixed in a subsequent release of Tyrus.

The client can bootstrap using Java SE or Java EE components as:

WebSocketContainer container = ContainerProvider.getWebSocketContainer();
String uri = "ws://localhost:8080" + request.getContextPath() + "/websocket";
container.connectToServer(MyClient.class, URI.create(uri));
The complete source code used in this Tip Of The Day (TOTD) is available here. It will run on the latest checked out version of GlassFish workspace, not a promoted build yet (bleeding edge eh ;-)

Please provide feedback on users@tyrus.java.net.

How are you going to use Java-based WebSocket client in your applications ?

Here are other Java EE 7 entries published on this blog so far:


And there was much rejoicing

Posted by Hal on February 13, 2013 at 06:22 PM PST #

I've been waiting for this a few months now! Thanks for letting us know it's coming. And the classes for the Java client are also in Tyrus?

Does Netbeans7.3 know how to handle these annotations yet?

I presume once we get a connection to the server we can connect to the ejb container as if using a local connection?

How would secure communications change your basic code? It's not too realistic that websocket will be used to connect to a localhost! Most of us write clients that will be connecting to a remote host.

Thanks a bunch for keeping us informed about the Java client for websocket.

Posted by guest on February 14, 2013 at 06:30 PM PST #

All the classes are in Tyrus and integrated in GlassFish.

NetBeans Development Version provide early support for Java EE 7 and is explained at:


Now this is bleeding edge, there is not even a promoted build to support this. So feel free to check out support in the IDE :-)

I will play with connection to remote host and then push that as a blog. EJBs should be accessible and thats for another blog entry :)

How do you plan to use this feature ?

Posted by Arun Gupta on February 15, 2013 at 09:08 AM PST #

My plan is to modernize my financial applications. I've been using RMI for 15 years and at the time I started I had to write everything for myself. If there were any free containers for learning I was unaware of them. Now, however, I want to start using the JEE ejb container to interface with my database and feed the data to my clients. Websockets "sounds" like it offers a great 2-way communication facility to create first rate clients. I cannot feature a day when I could conscionably ask my internal users to attempt to get their daily work done with something as crude as a browser. So I'm very interested in anything that modernizes java clients to use the facilities of JEE.

Please tell your friends over at Glassfish to work on the documentation regarding how to connect a Java client to a remote server including secure communications! Nobody needs to connect a java client to a local server in the real world!!! Thanks.

Posted by guest on February 15, 2013 at 12:18 PM PST #

Post a Comment:
Comments are closed for this entry.

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


« June 2016