Search code examples
websocketplayframework-2.0java-websocket

how to handle the online users in websocket


I have made a chat app over websocket protocol, now my question is how to handle the current online users, should i store their sessions(i.e user details like user_id) in hashMap or some other?


Solution

  • I don't see any other option besides storing this information as it is (most probably) important for your application. Imagine that you have the requirement to support private messages: you cannot just send the message to all connected clients.

    You should come up with some datastructure that holds your connection information. Maybe some map where the key is the session identifier (or the username, or some oder unique identifier) and the value is a pair of the WebSocket.In and WebSocket.out objects. You can even have two maps (one for the 'in' objects, one for the 'out' objects), whatever suits you. Then you can send a message to a specific client or just to a couple of them but not all.

    You can use the public void onReady(WebSocket.In<String> in, WebSocket.Out<String> out) method to put the data in the map. (here I assume that you are using Java and handle the WebSockets with callbacks)

    Of course, don't forget to clean up the map(s) when the socket is closed (look at the in.onClose() method).