1

I have Java Spring Application, that has Chat endpoint served though WebSocket. User logs in using API calls and session has timeout 10 mins.

In order to start Chat I have to connect to WebSocket endpoint. It does connect, no problem, but the thing is that: each chat message sent from client does not prolong logged in HttpSession, so after 10 mins it times out.

How can I make chat message via WebSocket connecting to prolong HttpSession? Other words saying: how can I reset HttpSession timeout timer to 10 mins on each message sent via WebSocket?

Using reflection method I get HttpSession from WebSocket session and then I call setMaxInactiveInterval() method to reset session timeout timer, but it does not work, session still times out after 10 mins, even if I send many messages in between.

@OnMessage
public void onMessage(Session session, String message) {
 HttpSession httpSession = getHttpSession(session);
 processMessage(message);
 int initialTimeout = httpSession.getMaxInactiveInterval(); // returns 600 (10 mins) 
 httpSession.setMaxInactiveInterval(initialTimeout);
}

I need to find the way Spring extends the session on each API call and probably do it same way. Does anyone knows how Spring does it?

asked Jul 24, 2017 at 18:41
3
  • Would you please post your code? How are you wanting to maintain your session? Commented Jul 24, 2017 at 18:49
  • just updated my original post Commented Jul 24, 2017 at 19:26
  • Ok I just understood, I am doing it wrong. The httpSession.setMaxInactiveInterval(initialTimeout) methods just sets max session expiration value, but does not resets the session countdown timer. I need to find different solution. Commented Jul 24, 2017 at 19:37

1 Answer 1

2

As you are not using HTTP when sending data through a WebSocket connection, the HTTP session will eventually timeout and this will also make your WebSocket connections close (as described in the JSR-356).

An easy solution to keep the HTTP session alive when using Spring WebSockets would be using Spring Session along.

answered Jul 24, 2017 at 21:55
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah Spring Session will work, but I have a big project that uses HttpSession, too late to refactor it. Is there a way to use only HttpSession ?
You can still use the HttpSession when using Spring Session, a filter replaces the HttpSession with a custom implementation backed by Spring Session

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.