canisrufus
canisrufus

Reputation: 693

How can I implement message passing between Django views?

Background: I'm trying to build a survey app where users can add answers to the survey and have these answers pushed to other users (e.g. if the question is "What is your favorite programming language?" and I didn't include Haskell, the user can add "Haskell" as an answer, and it will show up on everyone's browser). I'm planning to use long polling to achieve this.

My conceptual approach: The approach that I've settled on is setting up something like an observer pattern (or maybe it is exactly like the observer pattern. I'm new to design patterns).

The steps would be something like this: The browser makes a request to /app/longpoll, which spins up a view which 1. uses time stamps to check if any changes have occurred, and if so returns those changes, or 2. registers itself with an event delegator and waits for a message. Then when a browser makes a request to /app/UpdateSurvey a view gets fired up which 1. updates the survey, and 2. informs the event delegator that the survey has been changed.

My question: Given that this is a sane approach to solving this problem, how do I implement it? It seems like I need a standing process to serve as the event delegator, but I'm really not sure what this looks like. How do I find this process to register with it? How do I register with it? How do I make this process exist in the first place? What happens if this process is busy delegating events when a answer gets added to the poll?

I recognize that this is asking for a long answer, and has probably been solved by other people, so article/book recommendations are also encouraged.

Upvotes: 1

Views: 237

Answers (1)

cerberos
cerberos

Reputation: 8035

This is a different solution to the one you've asked for but how about using a post_save signal when a new question is added that uses websockets to push to any user who has the page open (subscribed clients)?

Edit: Ah, IE - use Juggernaut in place of websockets (will still use websockets if the browser supports them), works on IE6 (I think) and 7 (I know)

Upvotes: 1

Related Questions