node.js - Use redis to build a real time chat with socket.io and NodeJs -


i want built real time chat system project have problems redis because want data stored better possible.

my problem:

i'd use socket io real time chatting in closed group (of 2 people), how store messages?

redis key value store , means if want store need add unique key data before getting stored.

if same user posts more 1 messages keys use inside redis? i'm thinking unique ids unique keys since want able fetch comments when user log chat page, if need write database relate chat ids user posted message

am forgetting anything? there best method this?

sorry bad english.

redis more key-value store.

so want following:

  • chat messages,
  • two-person discussions,
  • you did not mention time constraints, lets assume archive messages after while,
  • you don't if want separate "threads" between 2 people, forums or continuous messages, facebook. i'm assuming continuous.

for each user, have store messages sends. let's app_namespace:messages:<user_id>:<message_id>. add userid here can retreive messages sent single user.

and, each 2 users, need track conversations. key, can use userids app_namespace:conversations:<user1_id>-<user2_id>. make sure same, shared conversation 2 users, can sort ids alfabetically, users 132 , 145 both have 132:145 conversation key

so store in "conversations"? let's use list: [messagekey, messagekey, messagekey].

ok, messagekey? combo of userid above , messageid (so can actual message).

so basically, need 2 things:

  1. store message , give id
  2. store reference message relevant conversation.

with node , standard redis/hiredis client somehting (i'll skip obvious error etc checks, , i'll write es6. if cannot read es6 yet, paste babel):

 // assuming init connects redis , exports redisclient import redisclient './redis-init'; import uuid `node-uuid`;   export function storemessage(userid, touserid, message) {    return new promise(function(resolve, reject) {      // give id.     let messageid = uuid.v4(); // gets random uid.     let messagekey = `${userid}:${messageid}`;     let key = `my_app:messages:${messagekey}`;     client.hmset(key, [       "message", message,       "timestamp", new date(),       "touserid", touserid     ], function(err) {       if (err) { return reject(err); }        // stored message. want store reference messagekey       let convokey = `my_app:conversations:${userid}-${touserid}`;        client.lpush(convokey, messagekey, function(err) {         if (err) { return reject(err); }         return resolve();       });     });   }); }  // need retreive messages users.  export function getconversation(userid, otheruserid, page = 1, limit = 10) {   return new promise(function(resolve, reject) {     let [userid1, userid2] = [userid, otheruserid].sort();     let convokey = `my_app:conversations:${userid1}-${userid2}`;     // lets sort out paging stuff.      let start = (page - 1) * limit; // we're zero-based here.     let stop = page * limit - 1;     client.lrange(convokey, start, stop, function(err, messagekeys) {        if (err) { return reject(err); }       // have message keys, messages.       let keys = messagekeys.map(key => `my_app:messages:${key}`);       let promises = keys.map(key => getmessage(key));       promise.all(promises)       .then(function(messages) {          // have them. can sort them          return resolve(messages.sort((m1, m2) => m1.timestamp - m2.timestamp));       })       .catch(reject);     });    }); }  // need getmessage here promise. have used promisify implementation hey. export function getmessage(key) {   return new promise(function(resolve, reject)  {     client.hgetall(key, function(err, message) {       if (err) { return reject(err); }       resolve(message);     });   }); } 

now that's crude , untested, that's gist of how can this.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -