Pages

December 28, 2023

#WebSockets: Part 3 Key components of the Java WebSocket API



In Part 1, we discussed some basic Socket programming interview questions. In Part 2 we discussed some basics of WebSockets. In Part 3 we will learn about the Java API for WebSocket (JSR 356), the key components of the Java WebSocket API, and how to handle WebSocket events in Java.
    
The Java API for WebSocket, specified by JSR 356, supports creating WebSocket applications in Java and is defined in the javax.websocket package. It provides a set of classes and interfaces that allow us to create WebSocket-based applications in Java. The WebSocket API was introduced in Java EE 7 and is designed to simplify the development of real-time, bidirectional communication between clients and servers.

Explain the key components of the Java WebSocket API.
The key components include Endpoint, Session, and MessageHandler.Here are some key classes and interfaces in the javax.websocket package:
  • javax.websocket.Endpoint: Endpoint represents the entry point for WebSocket communication. This is an abstract class that we extend to create your WebSocket endpoint. We override its methods to handle lifecycle events and incoming WebSocket messages.
  • javax.websocket.Session: Represents an active connection between the client and the server. We can use this object to send messages to the client and to manage the WebSocket session.
  • javax.websocket.MessageHandler: This is an interface we can implement to handle incoming WebSocket messages. There are three subinterfaces for handling different types of messages:
    • a). MessageHandler.Whole: Handles complete messages.
    • b). MessageHandler.Partial: Handles partial messages.
    • c). MessageHandler.String: Handles text messages.
  • javax.websocket.server.ServerEndpoint: An annotation we can use to declare a WebSocket endpoint on the server side. We attach this annotation to a class that extends Endpoint to define the URL where the WebSocket endpoint is available.
  • javax.websocket.server.ServerEndpointConfig: A configuration object for a WebSocket endpoint. We can use this to configure aspects of the endpoint, such as encoders, decoders, and extensions.
How can we configure Websocket endpoints, and how do you handle WebSocket events in Java?
There are two ways of configuring endpoints:
a). annotation-based: We can use dedicated method-level annotations.
b). extension-based: In this, we need to extend the javax.websocket.Endpoint class.

As the annotation model is a bit cleaner, that's why I preferred to use it. We can handle WebSocket endpoint lifecycle events with the following annotations:
  • @ServerEndpoint: If a class is annotated with @ServerEndpoint, the container ensures the availability of the class as a WebSocket server listening to a specific URI space.
  • @ClientEndpoint:  A class will be treated as a WebSocket client if it is annotated with @ClientEndpoint.
  • @OnOpen: If a Java method is annotated with @OnOpen, it will be triggered by the container when a new WebSocket connection is established.
  • @OnMessage: A Java method annotated with @OnMessage will be triggered by the container when a message is received from the client.
  • @OnError: A method with @OnError is invoked when there’s a problem with the communication.
  • @OnClose: A method annotated with @OnClose will be triggered when the WebSocket connection is closed.
What is the difference between text and binary messages in WebSockets?
  • WebSockets support two types of messages: text messages, which are encoded as UTF-8, and binary messages, which can contain any binary data. The choice between text and binary depends on the nature of the data being sent.
  • Text messages are encoded as UTF-8 and are meant for transmitting human-readable text. They are suitable for sending messages that consist of characters, strings, or JSON data. Whereas the Binary messages can be used to send arbitrary binary data. This can include images, files, or any other data that is not represented as text. Binary messages are not interpreted as UTF-8, allowing for the direct transmission of raw binary data.
  • Text messages may have a slightly larger overhead due to UTF-8 encoding, especially when dealing with non-ASCII characters. However, Binary messages are more efficient for transmitting raw binary data since there is no additional encoding overhead. 
Can you explain the role of Decoders and Encoders in the Java WebSocket API?
In the Java WebSocket API, encoders and decoders are components that help convert between WebSocket messages and Java objects. These components play a crucial role in the process of encoding Java objects into WebSocket messages before sending them over the network and decoding received WebSocket messages back into Java objects.

Decoders convert incoming messages to Java objects, while Encoders convert Java objects to outgoing messages.

In the next tutorial, we will create Server and Client endpoints, and learn how to handle different events in the WebSocket lifecycle.

If you enjoy our content and want to support us, please consider donating via  gpay, phonepay or paytm on +91 9920600280. Also, I’d appreciate if you’d buy me a coffee☕ 

https://www.buymeacoffee.com/greekykhs


Keep learning and growing!

-K Himaanshu Shuklaa..

No comments:

Post a Comment