Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Java WebSocket API: Difference Between Endpoint and RemoteEndpoint

DZone's Guide to

Java WebSocket API: Difference Between Endpoint and RemoteEndpoint

Wondering the differences between Endpoint and RemoteEndpoint? Here's a look at the class , which represents a WebSocket endpoint, and the interface.

· Integration Zone
Free Resource

The Integration Zone is brought to you in partnership with Cloud Elements.  What’s below the surface of an API integration? Download The Definitive Guide to API Integrations to start building an API strategy.

If you encounter the Endpoint and RemoteEndpoint artefacts from the Java WebSocket API for the first time, you might think they represent the same concept or you might even guess that they are hierarchical in nature. It is not the case.

javaee-logowebsocket_logo

Endpoint: The Class

javax.websocket.Endpoint is a simple (abstract) class which represents a WebSocket endpoint itself – either a server or a client endpoint. The Java WebSocket API itself provides both annotation and programmatic APIs to develop and design endpoints. An annotation based endpoint can be developed with the help of the following annotations

  • @ServerEndpoint or @ClientEndpoint
  • @OnMessage
  • @OnClose
  • @OnMessage and
  • @OnError

When you deploy an annotated WebSocket endpoint using Tyrus (WebSocket Reference Implementation), it internally creates an instance oforg.glassfish.tyrus.core.AnnotatedEndpoint which extends (and implements the abstractonOpen method)

tyrus-annotated-endpoint-impl

In case you want to use the programmatic API, you would need to extend the Endpoint class and override the (abstract) onOpen method yourself.

RemoteEndpoint: The Interface

WebSocket is just a protocol using which two parties (client and server) communicate.javax.websocket.RemoteEndpoint is an abstraction which represents the entity at the other end. It is available is two avatars

  • Synchronous: RemoteEndpoint.Basic
  • Asynchronous: RemoteEndpoint.Async

A RemoteEndpoint instance is encapsulated within the javax.websocket.Session object and can be obtained using the getBasicRemote or getAsyncRemote methods (for sync and async operation respectively). Here is the Tyrus implementation – org.glassfish.tyrus.core. TyrusRemoteEndpoint

Cheers!

The State of API Integration Report provides data from the Cloud Elements platform and will help all developers navigate the recent explosion of APIs and the implications of API integrations to work more efficiently in 2017 and beyond.

Topics:
enterprise integration ,integration ,ei

Published at DZone with permission of Abhishek Gupta, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}