Saturday, October 7, 2017

Websockets issue with django

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

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAJZFZXoOPTR4zfgA174m2_-s%3DJoFW9AW9q-Q7T1H59NtmkbQkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment