sockjs / sockjs/sockjs-node

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

Open
#272 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
2.1k
Forks
306
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read lib/session.js first, then trace how the client sessionId callback is handled by the SockJS server and how SockJSSession.bySessionId(sessionId) looks up sessions. Reproduce the disconnect and reconnect flow using the shown client and server snippets. Done means the session lookup behavior is explained and a focused regression test or documentation change captures the expected result.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.