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?
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.
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:
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:
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?