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


//annotated server endpoint with all its configuration elements
@ServerEnpdoint(
value = "/chat/",
configurator = ChatEndpointConfigurator.class,
decoders = JSONToChatObjectDecoder.class,
encoders = ChatObjectToJSONEncoder.class,
subprotocols = {"chat"}
)
public class ChatServer {
//server endpoint configuration in action
@OnOpen
public void onOpenCallback(Session session,
EndpointConfig epConfig) //injected by the WebSocket runtime
{
ServerEndpointConfig serverConfig = (ServerEndpointConfig) epConfig;
Map<String, Object> globalPropertiesMap = serverConfig.getUserProperties();
}
}

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


….
ServerEndpointConfig serverConfig = ServerEndpointConfig.Builder
.create(StockTrackerEndpoint.class , "/pop-stocks/").
.configurator(StockTrackerConfigurator.getInstance())
.decoders(JSONToStockTickerObject.class)
.encoders(StockTickerObjectToJSON.class)
.build();
….

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 subprotocols)

Also check out…

.. the new eBook – Java WebSocket API Handbook

Cheers!

About Abhishek

Loves Go, NoSQL DBs and messaging systems
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 )

Connecting to %s