Created
October 7, 2021 02:55
-
-
Save alex-hladun/e037aba8f5699a3c5e72b7a9b8bd2561 to your computer and use it in GitHub Desktop.
Querying the online user list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
getOnlineUsers = async () => { | |
const keys = new Set(); | |
let cursor = "0"; | |
do { | |
await new Promise((resolve, reject) => | |
this.redisClient.scan( | |
cursor, | |
"MATCH", | |
"session:*", | |
"COUNT", | |
"100", | |
(err, reply) => { | |
cursor = reply[0]; | |
reply[1].forEach((key: string) => keys.add(key)); | |
if (cursor === "0") { | |
resolve("ok"); | |
} | |
} | |
) | |
); | |
} while (cursor !== "0"); | |
const commands: any[] = []; | |
keys.forEach((key: unknown) => | |
commands.push(["hmget", key, "username", "connected"]) | |
); | |
return await new Promise((resolve, reject) => { | |
this.redisClient.multi(commands).exec((err, reply) => { | |
const userArray = reply.map((result: string[]) => mapSession(result)); | |
return resolve(userArray.filter((user) => user.connected === "true")); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment