Configuring WebSocket server endpoints

WebSocket Server endpoint configuration

Before we dive into the details, here is a quick snapshot of the related interfaces

Class/Interface Description
ServerEndpointConfig A derivative of EndpointConfig interface which is specific for configuration related to server side WebSocket endpoints
ServerEndpointConfig.Configurator An interface whose custom implementation allows sharing of global (for all endpoints) available logic/state as well opportunity to intercept the WebSocket handshake process (via method override)
ServerEndpointConfig.Builder Used only for programmatic server endpoints to build a ServerEndpointConfig instance

From here on, we’ll explore the configuration strategies for annotated and programmatic server endpoints

Configuring annotated server endpoints

Annotated server endpoints are configured implicitly via the elements of the @ServerEndpoint annotation. The WebSocket container picks up the value from the annotation elements and creates an instance of EndpointConfig behind the scenes. This instance is automatically injected (at run time by the WebSocket container) as a parameter of the @OnOpen method

Configuring programmatic server endpoints

Programmatic endpoints need (explicit) coding as far as configuration is concerned. This is because of the fact that programmatic endpoints are deployed differently and need an instance of ServerEndpointConfig

Please look at this blog post for details of the deployment aspects for programmatic WebSocket endpoints

Here is where the fluent builder ServerEndpointConfig.Builder comes into picture. Let’s look at an example which deomnstrates it’s usage

The big picture

Annotated and programmatic endpoint configuration are handled differently, but the end result is the same. Below is a table which shows the mapping b/w corresponding element of the @ServerEndpoint annotation, the corresponding method in ServerEndpointConfig as well as appropriate the method in the ServerEndpointConfig.Builder

@ServerEndpoint annotation element ServerEndpointConfig method ServerEndpointConfig.Builder method
value getPath() create(Class<?> endpointClass, String path)
configurator getConfigurator() configurator(ServerEndpointConfig.Configurator serverEndpointConfigurator)
decoders getDecoders() decoders(List<Class<? extends Decoder>> decoders)
encoders getEncoders() encoders(List<Class<? extends Encoder>> encoders)
subprotocols getSubprotocols() subprotocols(List<String> subprotocols)

Cheers!

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:

WordPress.com Logo

You are commenting using your WordPress.com 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