I'm trying to do a simple push protocol for django messages. So in my REST call, I have the following snippet:
storage = get_messages(self.request)
res = dict(messages=[dict(level=item.level, message=item.message) for item in storage])
now = monotonic()
while not res.messages and monotonic() - now < 15:
sleep(.5)
res.messages = [dict(level=item.level, message=item.message) for item in storage]
Naturally, the while loop does nothing because the messages framework simply re-reads the session variable which only "updates" on new requests.
I tried going into the underlying code to see if there was anything about updating the storage on the fly, but there seems to be no code that would do this, at least in the messaging framework. There was this promising undocumented storage.update()
method, but it turned out to do something else.
So, is there anything in Django framework that would allow me to poll for any messages changes and report that to the browser when it happens? Or a different method that would achieve the same thing more elegantly?