Java WebSocket API: difference b/w Endpoint and RemoteEndpoint

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-logo websocket_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 of org.glassfish.tyrus.core.AnnotatedEndpoint which extends (and implements the abstract onOpen method)


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


About Abhishek

Currently working as a Senior Product Manager in the Oracle Cloud Application Development team with a focus on Oracle Cloud PaaS portfolio. When not hovering in the clouds, I stay grounded with Java EE
This entry was posted in Java, Java EE and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s