Handling ‘state’ in Java WebSocket applications

By and large, there are two kinds of states in a WebSocket application

  • User/client specific: related to a connected user/Session e.g. user ID, list of subscriptions, last message received etc.
  • Global: state which is relevant across your application and something which all connected users/Sessions might be able to use

User specific state

This can be handled using getUserProperties method on the Session object – this exposes a Map which you can use to store anything (Object type) using a String type key

Global state

There are multiple options here as well. Please note that these are scoped to a specific Endpoint

  • getUserProperties in EndpointConfig – it exposes the same Map interface as the one in Session. Since the WebSocket runtime creates a single instance of an EndpointConfig object per Endpoint , it can be used a global state store

  • Another option is to encapsulate some of the common/global logic in a custom Configurator implementation which can be accessed & used within the endpoint logic

Further reading

Cheers!

Advertisements

About Abhishek

Java EE & distributed systems junkie who frequently blogs at abhirockzz.wordpress.com as well as simplydistributed.wordpress.com. Oh, I have also authored a few (mini) books, articles, Refcards etc. :-)
This entry was posted in Java, Java EE and tagged , , . Bookmark the permalink.

One Response to Handling ‘state’ in Java WebSocket applications

  1. Pingback: Handling ‘state’ in Java WebSocket applications | Ace Infoway

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