Redis
 sql >> डेटाबेस >  >> NoSQL >> Redis

Socket.io और NodeJs के साथ रीयल टाइम चैट बनाने के लिए रेडिस का उपयोग करें

रेडिस अधिक है तो की-वैल्यू स्टोर।

तो आप निम्नलिखित चाहते हैं:

  • चैट संदेश,
  • दो-व्यक्ति चर्चा,
  • आपने समय की कमी का उल्लेख नहीं किया है, इसलिए मान लें कि आप कुछ समय बाद संदेशों को संग्रहित करते हैं,
  • आप यह भी नहीं कहते कि क्या आप दो लोगों के बीच अलग-अलग "थ्रेड्स" चाहते हैं, जैसे फ़ोरम या निरंतर संदेश, जैसे फ़ेसबुक। मैं निरंतर मान रहा हूँ।

प्रत्येक उपयोगकर्ता के लिए, आपको उसके द्वारा भेजे गए संदेशों को संग्रहीत करना होगा। मान लें APP_NAMESPACE:MESSAGES:<USER_ID>:<MESSAGE_ID> . हम यहां उपयोगकर्ता आईडी जोड़ते हैं ताकि हम एक उपयोगकर्ता द्वारा भेजे गए सभी संदेशों को आसानी से पुनः प्राप्त कर सकें।

और, प्रत्येक दो उपयोगकर्ताओं के लिए, आपको उनकी बातचीत को ट्रैक करना होगा। एक कुंजी के रूप में, आप बस उनके उपयोगकर्ता आईडी APP_NAMESPACE:CONVERSATIONS:<USER1_ID>-<USER2_ID> का उपयोग कर सकते हैं . यह सुनिश्चित करने के लिए कि आप दोनों उपयोगकर्ताओं के लिए हमेशा समान, साझा वार्तालाप प्राप्त करें, आप उनकी आईडी को वर्णानुक्रम में क्रमबद्ध कर सकते हैं, ताकि उपयोगकर्ताओं 132 और 145 दोनों के पास वार्तालाप कुंजी के रूप में 132:145 हो

तो "बातचीत" में क्या स्टोर करें? आइए एक सूची का उपयोग करें:[messageKey, messageKey, messageKey]

ठीक है, लेकिन अब संदेश क्या है? ऊपर userId का कॉम्बो और एक messageId (ताकि हम वास्तविक संदेश प्राप्त कर सकें)।

तो मूल रूप से, आपको दो चीज़ें चाहिए:

  1. संदेश संगृहीत करें और उसे एक आईडी दें
  2. इस संदेश के संदर्भ को प्रासंगिक वार्तालाप के लिए संगृहीत करें।

नोड और मानक रेडिस / हायरडिस क्लाइंट के साथ यह कुछ ऐसा होगा (मैं स्पष्ट त्रुटि आदि जांच छोड़ दूंगा, और मैं ES6 लिखूंगा। यदि आप अभी तक ES6 नहीं पढ़ सकते हैं, तो बस इसे बेबेल में पेस्ट करें):

 // assuming the init connects to redis and exports a redisClient
import redisClient from './redis-init';
import uuid from `node-uuid`;


export function storeMessage(userId, toUserId, message) {

  return new Promise(function(resolve, reject) {

    // give it an id.
    let messageId = uuid.v4(); // gets us a 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); }

      // Now we stored the message. But we also want to store a reference to the messageKey
      let convoKey = `MY_APP:CONVERSATIONS:${userId}-${toUserId}`; 
      client.lpush(convoKey, messageKey, function(err) {
        if (err) { return reject(err); }
        return resolve();
      });
    });
  });
}

// We also need to retreive the messages for the 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); }
      // we have message keys, now get all messages.
      let keys = messageKeys.map(key => `MY_APP:MESSAGES:${key}`);
      let promises = keys.map(key => getMessage(key));
      Promise.all(promises)
      .then(function(messages) {
         // now we have them. We can sort them too
         return resolve(messages.sort((m1, m2) => m1.timestamp - m2.timestamp));
      })
      .catch(reject);
    }); 
  });
}

// we also need the getMessage here as a promise. We could also have used some Promisify implementation but hey.
export function getMessage(key) {
  return new Promise(function(resolve, reject)  {
    client.hgetall(key, function(err, message) {
      if (err) { return reject(err); }
      resolve(message);
    });
  });
}

अब यह कच्चा और परीक्षण नहीं किया गया है, लेकिन आप इसे कैसे कर सकते हैं इसका सार यही है।



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. रेडिस क्लस्टर शेयरिंग का परिचय - लाभ, सीमाएं, परिनियोजन और क्लाइंट कनेक्शन

  2. डेटाबेस के रूप में रेडिस

  3. रेडिस लेनदेन और लंबे समय तक चलने वाली लुआ स्क्रिप्ट

  4. हैश कुंजी नाम के आधार पर थोक में रेडिस हैश मान हटाएं

  5. सशर्त रेडिस सेट/केवल नवीनतम संस्करण के साथ अद्यतन?