# Retrieving Global Context Data in onConnection from ui\_base.js in Node-RED

**URL:** <https://discourse.nodered.org/t/retrieving-global-context-data-in-onconnection-from-ui-base-js-in-node-red/92100>\
**Category:** General\
**Tags:** node-red-dashboard\
**Created:** [1 October 2024 11:45 UTC](https://discourse.nodered.org/t/retrieving-global-context-data-in-onconnection-from-ui-base-js-in-node-red/92100 "2024-10-01T11:45:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nandhini-Subramaniya](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nandhini-subramaniya/32/92882_2.png) [@Nandhini-Subramaniya](https://discourse.nodered.org/u/Nandhini-Subramaniya)\
**Post date:** [1 October 2024 11:45 UTC](https://discourse.nodered.org/t/retrieving-global-context-data-in-onconnection-from-ui-base-js-in-node-red/92100/1 "2024-10-01T11:45:29Z")

</div>

I'm working with the ui\_base.js file in Node-RED and need to retrieve session data stored in the global context during the onConnection method of a socket connection. Normally, I would use the following code to retrieve session data:

javascript

let sessionData = global.get(sessionId);  
console.log("sessionData:", sessionData);

However, when I try this, I get the error: global.get is not a function.

How can I properly retrieve the session data from the global context within the ui\_base.js file during a socket connection? Is there a way to access the global context in this context or another approach I should be using?

Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [1 October 2024 12:19 UTC](https://discourse.nodered.org/t/retrieving-global-context-data-in-onconnection-from-ui-base-js-in-node-red/92100/2 "2024-10-01T12:19:24Z")

</div>

> [@Nandhini-Subramaniya](#):
>
> How can I properly retrieve the session data from the global context within the ui\_base.js file

Does't ui\_base run in the client browser session? You can't access context data (which is in the server) from within the browser, which may be the other side of the world.

---

<div class="post-metadata">

**Author:** ![Nandhini-Subramaniya](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/nandhini-subramaniya/32/92882_2.png) [@Nandhini-Subramaniya](https://discourse.nodered.org/u/Nandhini-Subramaniya)\
**Post date:** [2 October 2024 05:13 UTC](https://discourse.nodered.org/t/retrieving-global-context-data-in-onconnection-from-ui-base-js-in-node-red/92100/3 "2024-10-02T05:13:00Z")

</div>

no it is server side script.  
I'm working on a Node-RED project where I handle session management on the server side and store user details (such as session ID, username, and email) in the global context after a successful login. Here's the function I use to create and store the session on login:

```auto
if (user) {
    // Generate unique session ID
    const sessionId = generateSessionId(user.username);

    // Set the session ID as a cookie in the msg.headers
    msg.headers = {
        'Set-Cookie': `session_id=${sessionId}; HttpOnly; Max-Age=900`
    };
    console.log("User login cookies session:", msg.headers);

    // Store session information in global context
    global.set(sessionId, {
        username: user.username,
        email: user.email,
        loggedIn: true,
        sessionId: sessionId
    });

    msg.payload = {
        message: 'Login successful',
        username: user.username,
        email: user.email
    };
    msg.statusCode = 200;
} else {
    msg.payload = {
        error: 'Invalid credentials',
        redirect: 'http://localhost:1880/login'
    };
    msg.statusCode = 401;
}
return msg;

function generateSessionId(username) {
    return username + '-' + Math.random().toString(36).substr(2, 9) + Date.now();
}

```

After storing the session, I try to retrieve it in my `ui-base.js` file through a WebSocket connection like this:

```auto
function onConnection(socket) {
    uiShared.connections[socket.id] = socket;

    let cookies = socket.request.headers.cookie;
    console.log("Cookies in socket:", cookies);

    let sessionId = cookies.split(';').find(cookie => cookie.trim().startsWith('session_id='));

    if (sessionId) {
        sessionId = sessionId.split('=')[1];
        console.log("Session ID from the WebSocket connection:", sessionId);

        // Try to retrieve the session from global context
        let storedSession = global.get(sessionId);
        console.log("Stored session:", storedSession);
    } else {
        console.log("session_id not found in cookies");
    }

    emitConfig(socket);
    setupEventHandlers(socket, true);
}

```

The issue I'm facing is that when I attempt to retrieve the session using `global.get(sessionId)`, I get an error: **"global.get is not a function"**. I understand that this may be a scoping issue or something specific to Node-RED's handling of global context in different parts of the system, but I'm unsure how to resolve it.

Has anyone faced a similar issue, or does anyone know how I can properly access the global context from within this WebSocket connection code?

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [31 December 2024 05:13 UTC](https://discourse.nodered.org/t/retrieving-global-context-data-in-onconnection-from-ui-base-js-in-node-red/92100/4 "2024-12-31T05:13:34Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
