node.js - How to get the key from value in key/value pair in redis store client? -
i storing username/socketid pairs in redis store socket.io chat application.
when user disconnects need remove username/socketid pair redis store. have seen how value key never key value. possible? or either way how can delete key/value pair value. here's code
for adding store on connect
socket.on('username', function (username) { client.set(username, socket.id, function (err) { console.log(username + ":" + socket.id); }); });
for disconnect, client wont know when disconnect happen, might happen due loss of internet connectivity when socket disconnects hits "disconnect" event. in event need delete username/socketid pair.
socket.on('disconnect', function () { // dont know username?? client.del(username, socket.id, function (err) { if (err) console.log(err); else { socket.disconnect(); console.log(socket.id + " disconnected"); } }); });
the easiest way store two pairs. 1 username/id
, 1 id/username
. whatever information have, can other , result other key/value pair too.