See if users in node.js chat app have a matching message

Problem

I have a node.js chat app that uses socket.io and other dependencies but I have no db currently.

I would like some advice on how to approach this and how to implement it, as I'm working on an app where when 2 users at some point have a matching message, they don’t have to have the matching message at the same time.

User1 might of said the word "apple" at the start of the chat and user2 might of said apple half through the chat. When this match occurs they both get points. I've been looking around for a good base chat app that might facilitate this but nothing I've found has met that.

Thanks.

Problem courtesy of: lakers2012

Solution

This is how i could implement the whole game. Its clear that you will get the answers from both players. I guess that in your chat programm you'd have something of an identifier to check for the users ID and their chat session, so I'm not going to bother with this. I think that the implementation of sending a new "question" for their matching task shouldnt be a problem by just doing a simple

setTimeout(function(){ io.emit('newQuestion', {picture: randomPicture})}, 120000);

The string match should be another task. This could be easily done in javascript. As you are just trying to match complete strings, I would go about this as follows. Suppose you have a chat string1 and chat string2 corresponding to user1 and user2. From there you will need to cross reference all words in string1 with string2. For this string1 has to be broken apart.

var string1 = "apple half";
var string2 = "an apple";
var string1Split = string1.split(" ");

var match = false;

for(i=string1Split.length; i--;) {
 result = str.match(/string1Split[i]/g);
 if(result.length > 0 && match == false) {
  match = true;
 }
}

if(match) {
 //add points to it
}

I'm not sure if you have to replace the quotes ' " ' in your string1Split[i] but thats just a matter of doing another regexp.

Solution courtesy of: toxicate20

Discussion

It sounds like you could use arrays to store the tags, as they are sent to the server. At the same time you could check those incoming tags against the other user's stored tags.

I'd recommend redis for something like this. It's super fast, and it doesn't seem like you need to store data for long periods of time or keep in any kind of complicated data structure.

Discussion courtesy of: Costa

This recipe can be found in it's original form on Stack Over Flow.