[python-uk] Websockets issue with django

Samuel Muiruri muiruri.samuel at gmail.com
Sat Oct 7 15:51:24 EDT 2017


I have a websockets that's supposed to return data from a background
process running in celery, full details

Have a django project with django-channels (using redis) allows users to
start background process and should get back a live feed of this on the
page.

Here's a recording of this: https://youtu.be/eKUw5QyqRcs

Websockets disconnects with a 404.

Here's the websocket's code:

$('.test-parse').unbind().click(function() {
    ...
    $.ajax({
        url: "/start-render-part",
        type: "POST",
        dataType: 'json',
        beforeSend: function(xhr, settings) {
            if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
                // Send the token to same-origin, relative URLs only.
                // Send the token only if the method warrants CSRF protection
                // Using the CSRFToken value acquired earlier
                xhr.setRequestHeader("X-CSRFToken", csrftoken);
            }


        },
        data: JSON.stringify(data),
        success: function(response){
            if (response['status'] == 'ok') {
                if (response['unique-id']) {
                    unique_processing_id = response['unique-id'];
                }

                if (response['task_id']) {
                    task_id = response['task_id'];
                }


                var add_to_group_msg = JSON.stringify({
                    'unique_processing_id': unique_processing_id,
                    'task_id': task_id,
                    'command': 'add_to_group',
                });

                var socket = new
WebSocket("ws://{HOST}/render-part/".replace('{HOST}',
window.location.host));
                socket.onopen = function() {
                    console.log('opened');
                    var add_to_group_msg = JSON.stringify({
                        'unique_processing_id': unique_processing_id,
                        'task_id': task_id,
                        'command': 'add_to_group',
                    });
                    socket.send(add_to_group_msg);

                    var get_status_msg = JSON.stringify({
                        'task_id': task_id,
                        'unique_processing_id': unique_processing_id,
                        'command': 'check_status',
                    });
                    socket.send(get_status_msg);
                };

                socket.onmessage = function(event) {
                    console.log("onmessage.  Data: " + event.data);
                    var data = JSON.parse(event.data);
                    if (data.state == 'PROGRESS') {
                        console.log(data.status);
                        update_progress(data.current);
                    }
                    else if (data.state == 'FINISHED') {
                        var remove_from_group_msg = JSON.stringify({
                            'unique_processing_id': unique_processing_id,
                            'command': 'remove_from_group',
                        });
                        socket.send(remove_from_group_msg);
                        unique_processing_id = '';
                        task_id = '';
                    }
                };

                socket.onclose = function(event) {
                    if (event.wasClean) {
                        alert('Connection closed');
                    } else {
                        console.log('Connection terminated');
                    }
                    console.log('Code: ' + event.code + ' reason: ' +
event.reason);
                    console.log(event);
                };

                socket.onerror = function(error) {
                    console.log("Error " + error.message);
                };

                // Call onopen directly if socket is already open
                if (socket.readyState == WebSocket.OPEN) {
                    socket.onopen();
                }

                //End Sockets Code
            } //End for if rensponse['status'] == 'ok'


        },
        error: function(xhr){

        },


        });

    /* End Ajax Call */ });

possibly note worthy on local version the websockets get's to onopen which
I confirm thanks to the console output.

-- 

Best Regards,

Samuel Muiruri.

Web Designer | +254 738 940064
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-uk/attachments/20171007/c18f02f0/attachment-0001.html>


More information about the python-uk mailing list