javascriptpythongoogle-app-enginechannel-api

why so much XHR polling when using app engine Channel API


I am trying to use the App Engine Channel API to avoid polling for updates in my client. The problem is that I see an continuous stream of XHR packets sent in the Chrome console after starting a socket. They all say:

XHR finished loading: GET "http://localhost:8080/_ah/channel/devcommand=poll&channel=xxxOMITTEDxxx-channel-yyyOMITTEDyyy-zzzOMITTEDzzz-1&client=1". jsapi:5406goog.net.XhrIo.send jsapi:5406goog.net.XhrIo.send jsapi:5352goog.appengine.DevSocket.poll_

I would not expect any XHR messages until the server tries to send a message. I am using the Python dev_appserver.

Maybe I am doing something wrong in my Javascript. I am successfully requesting a token from the server. When my client receives the token, I start the socket like this:

function listen_to_channel(msg) {
  console.log('--- server response to channel request: ' + JSON.stringify(msg));

  // open a channel socket
  var channel = new goog.appengine.Channel(msg.token);

  var socket = channel.open();
  socket.onopen = function(){ console.log('socket.onopen')};
  socket.onmessage = function(msg){ console.log('socket.onmessage: ' + msg.data)};
  socket.onerror = function(err){ console.log('socket.onerror: ' + err.description + ', ' + err.code)};
  socket.onclose = function(){ console.log('socket.onclose')};
}

I run that code from jQuery, like so:

$(document).ready(function() {
    $.get('/admin/channel', {clientID:1}, listen_to_channel, 'json')
});

I link the dependencies like so:

<head>
  <script type="text/javascript" src="/_ah/channel/jsapi"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script src="javascript/admin.js"></script>
</head>

The example code is clear that the socket should be created within the tag of the HTML DOM. Is this the cause of my problem? If so, what does creating the socket within the tag do differently?


Solution

  • Polling is how the channel-API is simulated in the SDK, so what you're seeing is expected.