Thursday, June 30, 2011

Django AJAX polling: Best practice?

Hello everyone,

We have a game-application where several players should interact in
realtime (i.e. change attributes of a "game"-object, which is a django
model instance). To my knowledge, best / easiest method for this
problem would be AJAX-polling several times per second, to check
whether the game has changed or not since the last poll. To avoid
waste of bandwidth, we want of course only send the data that has
changed since the last poll, not the whole object over and over again.
We decided to use dajaxice and jquery.

However, I'm not really sure how to approach that task:
- One possiblity would be using the db, writing every change in there
and querying it for every poll. Maybe that game-instance will then get
cached if we use cache-middleware, but since I've never worked with
caching I'm not sure about this and don't feel confident with this
approach.
- The other possibility would be a global dict (or instance of an own
class), let's call it GAMES_ACTIVE, where each active game is stored
by its id. Then I could save the 'state' and the 'changes' of the game
there. So if player X changes game attributes that others would need
to poll, I could update the state and additionally append the new data
to a list of changes for each player that yet wasn't polled:
GAMES_ACTIVE[game.id]['state'] = game-instance, always up to date
GAMES_ACTIVE[game.id]['changes'][player_Y] = [list of not yet
polled changes for player Y]
As soon as Player Y polls the changes, they are sent to the client,
and the 'changes' for player Y is reset to an empty list []

Is this a good way to solve the problem? Any other suggestions?

Regards,
Andreas

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment