Skip to content

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

@ArmanNisch

Description

@ArmanNisch

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));
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions