I want to built a real time chat system for my project but actually I have some problems with Redis because I want my data stored as better as possible.
My problem:
I'd like to use Socket Io to do real time chatting in a closed group (of two people), but how to store messages?
Redis is a key value store and that means that if i want to store something i need to add an unique key to my data before getting stored.
If the same user posts more than one messages which keys would I use inside redis? I'm thinking about unique ids as unique keys but since I want to be able to fetch this comments when a user log the chat page, but if I do that I need to write another database that relate chat ids to the user that posted that message
Am I forgetting anything? Is there a best method to do this?
Sorry for my bad English.
Redis is more then key-value store.
So you want the following:
For each user, you have to store messages he sends. Let's say
APP_NAMESPACE:MESSAGES:<USER_ID>:<MESSAGE_ID>
. We add userId here so that we can easily retreive all messages sent by a single user.And, for each two users, you need to track their conversations. As a key, you can simply use their userids
APP_NAMESPACE:CONVERSATIONS:<USER1_ID>-<USER2_ID>
. To make sure you always get the same, shared conversation for the two users, you can sort their ids alfabetically, so that users 132 and 145 will both have 132:145 as conversation keySo what to store in "conversations"? Let's use a list:
[messageKey, messageKey, messageKey]
.Ok, but what is now the messageKey? Combo of userId above and a messageId (so we can get the actual message).
So basically, you need two things:
With node and standard redis/hiredis client this would be somehting like (I'll skip the obvious error etc checks, and I'll write ES6. If you cannot read ES6 yet, just paste it to babel):
Now that's crude and untested, but that's the gist of how you can do this.
Is redis is a constraint in your project?
you can go through this http://autobahn.ws/python/wamp/programming.html