0

I'm using the appengine channel api(with deferred tasks) but it doesn't seem to be working.

Here's my server code in a gist:

Class Handler(webapp2.RequestHandler):
  def get(self):
    path = jinja_environment.get_template('templates/new_console.html')
    token = channel.create_channel('some_key')
    # Deferring the task.
    deferred.defer(_task, token)
    args = {}
    args['token'] = token
    self.response.out.write(path.render(args))



def _task(token):
  FeedbackThreadModel(id='id').put()
  time.sleep(60)
  channel.send_message(token, 'done')

And here's my javascript client

<html>
  <head>
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    <script>
        channel = new goog.appengine.Channel('{{ token }}');
        socket = channel.open();
        socket.onmessage = onMessage;
        onMessage = function() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/secondpage');
            xhr.send();
        };
      </script>
  </head>

I expect the GET request to be initiated to the URL: '/secondpage' after the task is complete, but that's not happening. What am I doing wrong ?

Shan
  • 1,138
  • 17
  • 32
  • how long does your task actually take? have you tried delaying the task with a countdown, as it likely happens before the browser gets to create the channel. Also check the logs, both on the browser and the servers. What does actually happen? – Paul Collingwood Feb 11 '16 at 19:36
  • @PaulCollingwood Thanks, tried it but didn't help. – Shan Feb 12 '16 at 03:38

1 Answers1

0

This now seems to be working. Apparently the socket request javascript should be in html body and not in head. The working javascript looks like this:

<html>
  <body>
    <script type="text/javascript" src="/_ah/channel/jsapi"></script>
    <script>
        channel = new goog.appengine.Channel('{{ token }}');
        socket = channel.open();
        socket.onmessage = function() {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/secondpage');
            xhr.send();
        };
      </script>
  </body>
Shan
  • 1,138
  • 17
  • 32