So I am trying to write a chat system using django (I am relatively new to real time system). I did some research - there are lots of options (twisted, tornado etc) but for now I decided to to try and use nginx as the web server and redis's pubsub.
The chat would be between two users at a time.
Following is what I was thinking of:
On authentication all users issue a psubscribe chatctrl:*:. This essentially subscribes to a control channel to establish the initial conversation that is always needed
When a user u1 launches a chat with user u2, we
create a channel, say "chat:u1:u2" and subscribe to it.
The user u1 publishes a message to the control channel chatctrl:u1:u2: (a control message that would be listened to by u2) says effectively "do you want to chat with me on channel "chat:u1:u2"?
- The user u2 should get this message, subscribes to the channel and responds as yes via another message on control channel (or on the newly established channel.
- A session is established and both users can publish to the same channel and listen to it as well.
My question is: 1. Does the above make sense, first of all? If not how would you do it using redis? 2. The second question is where do I put the loop to listen to the messages. Since it would be "blocking" when there are no messages, it can not go in a view or in a model accessed by a view. Should it be in a spawned thread and if so how do I unsubscribe once the chat session is over?
Thanx!