javascriptgoogle-app-enginechannel-api

Debugging Channel API Appengine


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 ?


Solution

  • 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>