0

I'm using django-dajaxice-ng package (django-dajaxice fork for Django 1.5 and above, since I use Django 1.5.4), and I have this strange issue.
Sometimes the callback function is called twice or even more.
Simple usage:

...
{% load dajaxice_templatetags %}
...
{% dajaxice_js_import %}

<script>

    function update_chat(data) {
        messages = eval(data.messages);
        howmany=messages.length;
        if (howmany>0)
            console.log('called');
        for (i=0;i<howmany;i++) {
            M=messages[i];
            console.log(M.msg,M.sent);
        };
    }

    setInterval(function() {
            Dajaxice.myapp.receiveChat(update_chat,{'userID': {{otheruser.id}} });
    },2000);

 </script>

and in myapp/ajax.py:

def receiveChat(request,userID):
    # find new messages from userID and mark them as READ.
    return json.dumps({'messages':serialized_messages})

Here's the console log:

called
"Message" "16:09:28"
called
"Message" "16:09:28" 

As you can see, sometimes (very often, actually) update_chat is triggered 2 or 3 times.
I'm 100% sure that:

  • the ajax function receiveChat is called once
  • the message shown twice is stored once.

I define serialized_messages = json.dumps(messages), being messages a list of dicts.
The result is like this:

'[{"msg": "Message", "sent": "16:09:28"}]'

In this case there's one only message, but works as well with more than one.

I had to do this double serialization (the other one's in return) because apparently dajaxice accept only dict as admitted return values.

Anyone who also got this behavior? Any clue?

EDIT I'm using neo4django which uses neo4j-rest-client, I have to do a .save() on a model, and notice ONLY with that save I got the problem, maybe because of another HTTP request?

EDIT 2 It's not a dajaxice issue, switching to ajax $.get() gives same results, but really I can't figure it out. Seems like not all browsers are doing it, sure Chrome does.

6
  • have you checked whether your sendChat function is called more than once? Mar 18, 2014 at 13:48
  • Yep, and no ;) that function is the one storing the message, and as I wrote, it's stored once because the function is called once. 100% sure is the callback.
    – tonjo
    Mar 18, 2014 at 14:13
  • maybe posting much of the js would help to figure this out. i never experienced such behavior with django-dajaxice. Mar 18, 2014 at 14:40
  • Actually I edited the question because the relevant part was not in SENDING, but RECEIVING the messages.
    – tonjo
    Mar 18, 2014 at 14:50
  • Added javascript code (for debugging I'm simply showing chat messages in console.log).
    – tonjo
    Mar 18, 2014 at 15:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.