Task: There is a server implemented using the Helidon SE4. I need to store user data associated with each WebSocket connection on the server side.
Problem: Unlike Jakarta WebSockets, Helidon SE 4 does not provide any mechanisms for this. WsSession
does not have methods like Session.getUserProperties()
, and the class implementing WsListener
is reused for all connections established.
What I tried to do: But each WebSocket connection is provided with its own unique virtual thread.
ThreadLocal
to store user data. However, this leads to memory leaks because the client can close the connection without notifying the server. As a result, none of the WsListener
methods will ever be called from the desired thread, making it impossible to call ThreadLocal.remove()
from the desired thread.ConcurrentHashMap
. But this caused performance problems because all threads responsible for the connection are forced to constantly wait for each other.If you use WsRouting.Builder
the endpoint method can take a supplier which is ran once per request to return a listener.