DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Structured Logging
  • Effective Java Collection Framework: Best Practices and Tips
  • Understanding Dependencies...Visually!
  • How To Approach Java, Databases, and SQL [Video]

Trending

  • Structured Logging
  • Effective Java Collection Framework: Best Practices and Tips
  • Understanding Dependencies...Visually!
  • How To Approach Java, Databases, and SQL [Video]
  1. DZone
  2. Coding
  3. Languages
  4. WebSocket Implementation in Tomcat 7 and Jaggery

WebSocket Implementation in Tomcat 7 and Jaggery

Madhuka  Udantha user avatar by
Madhuka Udantha
CORE ·
Feb. 28, 14 · Interview
Like (1)
Save
Tweet
Share
9.61K Views

Join the DZone community and get the full member experience.

Join For Free

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

Apache Tomcat WebSocket Implementation

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

Opinions expressed by DZone contributors are their own.

Trending

  • Structured Logging
  • Effective Java Collection Framework: Best Practices and Tips
  • Understanding Dependencies...Visually!
  • How To Approach Java, Databases, and SQL [Video]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: