IO-2935-Add-Enhanced-Websocket-Provider - PR notes, Admin UI, Expanded Join/Leave room functionality, moved test button to the client id (Imex / Rome) in footer, so it is still there, just invisible.
Signed-off-by: Dave Richer <dave@imexsystems.ca>
This commit is contained in:
177
server/utils/redisHelpers.js
Normal file
177
server/utils/redisHelpers.js
Normal file
@@ -0,0 +1,177 @@
|
||||
/**
|
||||
* Apply Redis helper functions
|
||||
* @param pubClient
|
||||
* @param app
|
||||
*/
|
||||
const applyRedisHelpers = (pubClient, app) => {
|
||||
// Store session data in Redis
|
||||
const setSessionData = async (socketId, key, value) => {
|
||||
await pubClient.hSet(`socket:${socketId}`, key, JSON.stringify(value)); // Use Redis pubClient
|
||||
};
|
||||
|
||||
// Retrieve session data from Redis
|
||||
const getSessionData = async (socketId, key) => {
|
||||
const data = await pubClient.hGet(`socket:${socketId}`, key);
|
||||
return data ? JSON.parse(data) : null;
|
||||
};
|
||||
|
||||
// Clear session data from Redis
|
||||
const clearSessionData = async (socketId) => {
|
||||
await pubClient.del(`socket:${socketId}`);
|
||||
};
|
||||
|
||||
// Store multiple session data in Redis
|
||||
const setMultipleSessionData = async (socketId, keyValues) => {
|
||||
// keyValues is expected to be an object { key1: value1, key2: value2, ... }
|
||||
const entries = Object.entries(keyValues).map(([key, value]) => [key, JSON.stringify(value)]);
|
||||
await pubClient.hSet(`socket:${socketId}`, ...entries.flat());
|
||||
};
|
||||
|
||||
// Retrieve multiple session data from Redis
|
||||
const getMultipleSessionData = async (socketId, keys) => {
|
||||
const data = await pubClient.hmGet(`socket:${socketId}`, keys);
|
||||
// Redis returns an object with null values for missing keys, so we parse the non-null ones
|
||||
return Object.fromEntries(keys.map((key, index) => [key, data[index] ? JSON.parse(data[index]) : null]));
|
||||
};
|
||||
|
||||
const setMultipleFromArraySessionData = async (socketId, keyValueArray) => {
|
||||
// Use Redis multi/pipeline to batch the commands
|
||||
const multi = pubClient.multi();
|
||||
|
||||
keyValueArray.forEach(([key, value]) => {
|
||||
multi.hSet(`socket:${socketId}`, key, JSON.stringify(value));
|
||||
});
|
||||
|
||||
await multi.exec(); // Execute all queued commands
|
||||
};
|
||||
|
||||
// Helper function to add an item to the end of the Redis list
|
||||
const addItemToEndOfList = async (socketId, key, newItem) => {
|
||||
try {
|
||||
await pubClient.rPush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
|
||||
} catch (error) {
|
||||
console.error(`Error adding item to the end of the list for socket ${socketId}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to add an item to the beginning of the Redis list
|
||||
const addItemToBeginningOfList = async (socketId, key, newItem) => {
|
||||
try {
|
||||
await pubClient.lPush(`socket:${socketId}:${key}`, JSON.stringify(newItem));
|
||||
} catch (error) {
|
||||
console.error(`Error adding item to the beginning of the list for socket ${socketId}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
// Helper function to clear a list in Redis
|
||||
const clearList = async (socketId, key) => {
|
||||
try {
|
||||
await pubClient.del(`socket:${socketId}:${key}`);
|
||||
} catch (error) {
|
||||
console.error(`Error clearing list for socket ${socketId}:`, error);
|
||||
}
|
||||
};
|
||||
|
||||
// Add methods to manage room users
|
||||
const addUserToRoom = async (bodyshopUUID, user) => {
|
||||
await pubClient.sAdd(`bodyshopRoom:${bodyshopUUID}`, JSON.stringify(user));
|
||||
};
|
||||
|
||||
const removeUserFromRoom = async (bodyshopUUID, user) => {
|
||||
await pubClient.sRem(`bodyshopRoom:${bodyshopUUID}`, JSON.stringify(user));
|
||||
};
|
||||
|
||||
const getUsersInRoom = async (bodyshopUUID) => {
|
||||
const users = await pubClient.sMembers(`bodyshopRoom:${bodyshopUUID}`);
|
||||
return users.map((user) => JSON.parse(user));
|
||||
};
|
||||
|
||||
const api = {
|
||||
setSessionData,
|
||||
getSessionData,
|
||||
clearSessionData,
|
||||
setMultipleSessionData,
|
||||
getMultipleSessionData,
|
||||
setMultipleFromArraySessionData,
|
||||
addItemToEndOfList,
|
||||
addItemToBeginningOfList,
|
||||
clearList,
|
||||
addUserToRoom,
|
||||
removeUserFromRoom,
|
||||
getUsersInRoom
|
||||
};
|
||||
|
||||
Object.assign(module.exports, api);
|
||||
|
||||
app.use((req, res, next) => {
|
||||
req.sessionUtils = api;
|
||||
next();
|
||||
});
|
||||
|
||||
// // Demo to show how all the helper functions work
|
||||
// const demoSessionData = async () => {
|
||||
// const socketId = "testSocketId";
|
||||
//
|
||||
// // Store session data using setSessionData
|
||||
// await exports.setSessionData(socketId, "field1", "Hello, Redis!");
|
||||
//
|
||||
// // Retrieve session data using getSessionData
|
||||
// const field1Value = await exports.getSessionData(socketId, "field1");
|
||||
// console.log("Retrieved single field value:", field1Value);
|
||||
//
|
||||
// // Store multiple session data using setMultipleSessionData
|
||||
// await exports.setMultipleSessionData(socketId, { field2: "Second Value", field3: "Third Value" });
|
||||
//
|
||||
// // Retrieve multiple session data using getMultipleSessionData
|
||||
// const multipleFields = await exports.getMultipleSessionData(socketId, ["field2", "field3"]);
|
||||
// console.log("Retrieved multiple field values:", multipleFields);
|
||||
//
|
||||
// // Store multiple session data using setMultipleFromArraySessionData
|
||||
// await exports.setMultipleFromArraySessionData(socketId, [
|
||||
// ["field4", "Fourth Value"],
|
||||
// ["field5", "Fifth Value"]
|
||||
// ]);
|
||||
//
|
||||
// // Retrieve and log all fields
|
||||
// const allFields = await exports.getMultipleSessionData(socketId, [
|
||||
// "field1",
|
||||
// "field2",
|
||||
// "field3",
|
||||
// "field4",
|
||||
// "field5"
|
||||
// ]);
|
||||
// console.log("Retrieved all field values:", allFields);
|
||||
//
|
||||
// // Add item to the end of a Redis list
|
||||
// await exports.addItemToEndOfList(socketId, "logEvents", { event: "Log Event 1", timestamp: new Date() });
|
||||
// await exports.addItemToEndOfList(socketId, "logEvents", { event: "Log Event 2", timestamp: new Date() });
|
||||
//
|
||||
// // Add item to the beginning of a Redis list
|
||||
// await exports.addItemToBeginningOfList(socketId, "logEvents", { event: "First Log Event", timestamp: new Date() });
|
||||
//
|
||||
// // Retrieve the entire list (using lRange)
|
||||
// const logEvents = await pubClient.lRange(`socket:${socketId}:logEvents`, 0, -1);
|
||||
// console.log("Log Events List:", logEvents.map(JSON.parse));
|
||||
//
|
||||
// // **Add the new code below to test clearList**
|
||||
//
|
||||
// // Clear the list using clearList
|
||||
// await exports.clearList(socketId, "logEvents");
|
||||
// console.log("Log Events List cleared.");
|
||||
//
|
||||
// // Retrieve the list after clearing to confirm it's empty
|
||||
// const logEventsAfterClear = await pubClient.lRange(`socket:${socketId}:logEvents`, 0, -1);
|
||||
// console.log("Log Events List after clearing:", logEventsAfterClear); // Should be an empty array
|
||||
//
|
||||
// // Clear session data
|
||||
// await exports.clearSessionData(socketId);
|
||||
// console.log("Session data cleared.");
|
||||
// };
|
||||
//
|
||||
// if (process.env.NODE_ENV === "development") {
|
||||
// demoSessionData();
|
||||
// }
|
||||
|
||||
return api;
|
||||
};
|
||||
module.exports = applyRedisHelpers;
|
||||
Reference in New Issue
Block a user