Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ArmanNisch opened this issue Mar 9, 2020 · 3 comments

Comments

@ArmanNisch
Copy link

ArmanNisch commented Mar 9, 2020

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
Copy link
Contributor

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
Copy link
Author

ArmanNisch commented Mar 9, 2020

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
Copy link
Contributor

brycekahle commented Mar 9, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants