Over a million developers have joined DZone.

WebSocket Implementation in Tomcat 7 and Jaggery

· Integration Zone

Is iPaaS solving the right problems? Not knowing the fundamental difference between iPaaS and dPaaS could cost you down the road. Brought to you in partnership with Liaison Technologies.

This article will cover:

  1. Evolution of web communication
  2. Web socket
  3. Web socket implemented in Apache Tomcat 7
  4. How we Develop Web socket feature for jaggery

Tomcat 7 introduced a WebSocket implementation.  introduced a WebSocket implementation. Firstly I will give web socket's  benefits and limitations plus briefly web socket implemented in Apache Tomcat 7.

WebSocket is considered an evolution of web communication as shown below:

image

Think you know that Tomcat 6 implements bi-directional communication over HTTP using its Comet Processor. Here is limitation that it came across

  • Given that HTTP was intended to be request/response protocol rather than a bi directional protocol
  • Proxies and other intermediaries may not work properly
  • Only forward packets in one direction at any given time
  • Non-standard APIs
  • Multi threading for the servlet developers not easy

The Servlet 3.0[1] release introduced a new feature called Asynchronous Servlets. We can compare this feature to the server version of client AJAX calls. It lets a request hang until a response is ready to be delivered without using worker thread on the container. WebSockets is another attempt to standardize (JavaScript AP) an idea of having asynchronous, event driven and bi-directional communication over HTTP.

What web socket will give us

  • Bi-directional communication over a HTTP port by upgrading/switching protocols is major difference(Initiated by a HTTP request)
  • Message/Frame based communication
  • Intended to work with proxies and intermediaries
  • Still not the proxies and intermediaries working

Some to to know for the best

  • WebSocket are ideal for longer running conversations between client and server.
  • WebSocket is a straightforward implementation of bi-directional communication over HTTP.
  • Since WebSocket is a protocol sent over TCP after an initial HTTP handshake. You really can only do two things:
    • Send messages
    • Receive messages

Here WS look like.

image

  • “Connection:upgrade” headers.
  • The response contains the critical status code of 101.
  • Indicating that a protocol switch has been confirmed and is now expected.

After this handshake is complete the client and server move away from request/response communication and can send messages independently of each other. Here is my frames

image

How Apache Tomcat Implemented WebSocket

1. To get started with WebSocket you will have to extend Tomcat’s WebSocket class[2].

2. Writing you know class extend the WebSocketServlet class (Since it’s a servlet, you must map it to a URL)
3. Implement a message listener since extending the WebSocketServlet requires us to implement createWebSocketInbound()
It can be implemented to be notified of events. The two required methods are

  • protected void onBinaryData(InputStream inStream);
  • protected void onTextData(Reader reader);

4. If you wish to be notified when a WebSocket connection is opened or closed, simply override the onOpen and onClose methods

  • @Override
    protected void onOpen(WsOutbound outbound);
  • @Override
    protected void onClose(int status);

5. Writing data to the client

5.1. We have StreamInbound implementation will have a reference to the sender component, WsOutbound. You simply retrieve it by calling

  • myStreamInbound.getWsOutbound()

at that point you can send either binary

  • public void writeBinaryData(int b);
  • public void writeBinaryMessage(ByteBuffer msgBb);

or textual data to the client

  • public void writeTextData(char c);
  • public void writeTextMessage(CharBuffer msgBb);

[NOTE]
These methods are mutually exclusive.  Don’t call both of these methods and expect both binary and textual data to be sent.

Closing the connection to the client

There are two ways a channel can close in both a “clean” and a “not clean” manner. Clean implies that a handshake has been completed over TCP. Not clean closure means that the TCP connection was disconnected or aborted prior to the close handshake taking place.

  • public synchronized void close(int status, ByteBuffer data)

To be notified of when a channel has been closed by the client or uncleanly, you override with.

  • protected void onClose(int status);

Jaggery Implementation for the web socket

Here is sequence diagram of jaggery Web socket implementation

image

Next Post I will give how to used jaggery web socket in Chat application.

Still Above implementation can not see in jaggery as I have not commit it, But in tomorrow (3/25/2014)it will be there.

[1] http://jcp.org/en/jsr/detail?id=315

[2] http://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/catalina/websocket/WebSocketServlet.html

Discover the unprecedented possibilities and challenges, created by today’s fast paced data climate and why your current integration solution is not enough, brought to you in partnership with Liaison Technologies.

Topics:

Published at DZone with permission of Madhuka Udantha, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}