1

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:

  1. On authentication all users issue a psubscribe chatctrl:*:. This essentially subscribes to a control channel to establish the initial conversation that is always needed

  2. When a user u1 launches a chat with user u2, we

  3. create a channel, say "chat:u1:u2" and subscribe to it.

  4. 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"?

  5. 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.
  6. 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!

S Seer
  • 71
  • 5

1 Answers1

0

See my answer here for an example of the system you describe.

In that code, the view spawns a Gevent greenlet that subscribes to Redis and pushes messages out to the client browser over socket.io.

The view then blocks until a message is recieved over socket.io, repeating for the duration of the chat session.

Hope that helps!

Community
  • 1
  • 1
bcattle
  • 12,115
  • 6
  • 62
  • 82