Over a million developers have joined DZone.

Running WebSocket Applications on TomEE: Step-by-step Tutorial

DZone's Guide to

Running WebSocket Applications on TomEE: Step-by-step Tutorial

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

WebSocket is a relatively new protocol and tool component for web application development. It allows you to create applications without any HTTP overhead. The main benefits of using WebSockets are:

  • A real-time, full duplex communication between the client and the server. This means that the client and server can communicate in both routes at ones.
  • A single interface for application development, which allows  you to save a lot of your time for development.
  • Applications can handle more concurrent users and a greater message volume, with less infrastructure.
  • WebSockets can replace long polling technology, which is also used for real-time products.

In this tutorial you’ll see how WebSocket technology works in the cloud; to be more exact, in the cloud with a highly available cluster.

Create environment

1. Log in into the Jelastic dashboard and click Create environment.

2. Select TomEE as your application server and specify the cloudlet limit for it. Switch on High Availability to provide perfect fail-over capabilities for your environment and Public IPv4 to enable WebSockets to work properly.

TomEE websocket tutorial

2. After switching on high availability (HA) NGINX load balancer will be added to your environment topology automatically. Click on NGINX, specify the cloudlet limit for it and switch on Public IP, which is needed for your WebSocket Java application functionality. Then specify the name of your environment and click on the Create button.

NGINX websocket env

In a few minutes your highly available environment with TomEE will be created.

TomEE Java websockets

Create application

1. We use Echo for testing, a simple Java application bundled into Tomcat 7. It sends simple messages to the server which echoes it back. Here’s the code:

package websocket.echo;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
public class EchoMessage extends WebSocketServlet {
private static final long serialVersionUID = 1L;
 private volatile int byteBufSize;
 private volatile int charBufSize;
 public void init() throws ServletException {
 byteBufSize = getInitParameterIntValue("byteBufferMaxSize", 2097152);
 charBufSize = getInitParameterIntValue("charBufferMaxSize", 2097152);
public int getInitParameterIntValue(String name, int defaultValue) {
 String val = this.getInitParameter(name);
 int result;
 if(null != val) {
 try {
 result = Integer.parseInt(val);
 }catch (Exception x) {
 result = defaultValue;
 } else {
 result = defaultValue;
return result;
 protected StreamInbound createWebSocketInbound(String subProtocol,
 HttpServletRequest request) {
 return new EchoMessageInbound(byteBufSize,charBufSize);
private static final class EchoMessageInbound extends MessageInbound {
public EchoMessageInbound(int byteBufferMaxSize, int charBufferMaxSize) {
 protected void onBinaryMessage(ByteBuffer message) throws IOException {
 protected void onTextMessage(CharBuffer message) throws IOException {

2. Now you have to package your application into a WAR file for its further deployment.

You can download this package with a few Tomcat websocket samples for testing.

Deploy application

1. Go back to the Jelastic dashboard and upload your app to the deployment manager.

Java websockets upload

2. Deploy the uploaded package to your environment. If you use Tomcat sample application like we do, deploy the package to the context, named examples.

TomEE websocket server deploy

Now you can open your application in a web browser using Public IPs of your TomEE servers and test it.

TomEE websocket server application

See!  It’s not complicated and your WebSocket works like a charm.

The instruction above is fully suitable for Tomcat 7 as well.

As you can see it’s quite easy to get your WebSocket up and running on Jelastic with its rich set of tools. If you have had any experience with WebSockets on our platform, please give us your feedback in the comments section below.

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}