sockjs / sockjs-node

WebSocket emulation - Node.js server
http://sockjs.org
MIT License
2.1k stars 309 forks source link

Not able to retrieve the same session (sessionID) in SockJS (Websocket) NodeJS Server #272

Open ArmanNisch opened 4 years ago

ArmanNisch commented 4 years ago

I am trying to persist server side sessions, even after the client has disconnected.

I have a standard SockJS client and sockjs-node setup.

The idea is that if a Client connects, then disconnects, and then re-connects to the server; the client should get back the same session from the server on every connection.

To try and achieve this, I am currently generating a random session ID string on the client side then I am passing this ID to the sockJS server.

The problem I am facing is that the Server Returns null when I pass the sessionID to the server to try and retrieve an existing session.

As far as I can see, this is how SockJS implements session persisting: https://github.com/sockjs/sockjs-node/blob/master/lib/session.js

I don't quite understand why I am getting back null, the session should be maintained on the server...?

I am trying to persist server side sessions, even after the client has disconnected.

I have a standard SockJS client and sockjs-node setup.

The idea is that if a Client connects, then disconnects, and then re-connects to the server; the client should get back the same session from the server on every connection.

To try and achieve this, I am currently generating a random session ID string on the client side then I am passing this ID to the sockJS server.

The problem I am facing is that the Server Returns null when I pass the sessionID to the server to try and retrieve an existing session.

As far as I can see, this is how SockJS implements session persisting: https://github.com/sockjs/sockjs-node/blob/master/lib/session.js

I don't quite understand why I am getting back null, the session should be maintained on the server...?

This is how I currently send the ID to the SockJS Server:

// (1) How I currently send the ID to the SockJS Server.
const socket = SockJS("${apiServerURL}/socket/auth/login", null, {
  sessionId: () => {
    // (2) Check if a session ID already exhists
    let sessionId = localStorage.getItem("sockjsSessionId");

    if(sessionId === null || sessionId === undefined) {

      // (3) If Session ID does not exist, create a random ID.
      sessionId = getRandomStringWithLength(8);
      localStorage.setItem("sockjsSessionId", sessionId);
    }

    // (4) Pass session id to SockJS Server
    return sessionId;
  }
})

This is how I currently am trying to retrieve the same session on the Server:

if (sessionId !== null && typeof sessionId !== "undefined") {
  const socketSession: SockJSSession | null = SockJSSession.bySessionId(sessionId);
  console.log("found sockjs session connection", socketSession);
  if (socketSession !== null) {
    socketSession.connection.write(JSON.stringify(response));
  }
}
brycekahle commented 4 years ago

Sessions are an internal construct and not intended to be manipulated outside the library. They automatically unregister post-disconnect after a certain delay. Changing them to persist long-term would break many assumptions and likely cause other issues like socket and memory leaks.

ArmanNisch commented 4 years ago

Ok, but what about this scenario: what if a frontend might be waiting for a computationally heavy response form the server, then and for some reason the frontend is disconnected and then reconnected. In this scenario, does the frontend have to make a new session with the server and requested the data again, because the first session has now been destroyed by the socksJS server?

Would there not be a way to retrieve the older session to get the original, computationally heavy, response from the server?

brycekahle commented 4 years ago

I suggest you have your own session ID that you send through the SockJS channel upon connect. Your initial idea seems fine, but don't use SockJS internals to accomplish it. It should live outside of the library. You may want to look into libraries such as https://github.com/primus/primus that layer on top of real-time transports to provide additional functionality.