Search code examples
javawebsocketendpointjava-websocket

Store data in ServerEndpoint


I'm developing a simple app with WebSockets. Client side you can send input texts, while the server side is implemented via a Java class with annotated as ServerEndpoint. Here the input texts received get decoded and sent to all of the open sessions.

This works, but I want to store every message received by the server, so that when a new client connects, (using a method annotated with @OnOpen) it receives all the messages that has been sent while he was "offline". Where can I store this messages without using a database?


Solution

  • Solved it by using Singleton pattern. I created a Singleton class that stores a List, which gets updated in the onMessage method. Then, in onOpen, the messages stored get sent to the client that just opened the connection with the socket.