title: send downstream message to google ccs with node js
body: <p>I am using <a href="https://github.com/node-xmpp/node-xmpp">node-xmpp</a> to connect to google gcm ccs servers. I followed the <a href="https://www.google.com/url?q=https://gist.github.com/jamesvnz/6756208&amp;sa=D&amp;sntz=1&amp;usg=AFQjCNEiaHKz8ttVGhwS_KCPeiJkbiCh2w">from gcm google groups</a> to connect. Now i need to send a downstream message whenever i receive a message from my redis subscriber(i subscribed to a redis channel <a href="https://www.npmjs.org/package/redis">redis node package</a>). My code is as follows </p>
  
  <pre><code>var gearmanode = require('gearmanode');
  var redis = require("redis");
  var xmpp = require('node-xmpp');
  
  var gearJob;
  var redisSubChan = 'test_channel';
  var gearmanJobName = 'reverse';
  var jobPayload;
  var redisClient;
  var xmppClient;
  var gearClient;
  
  gearClient = gearmanode.client();
  
  var options = {
      type: 'client',
      jid: 'myid@gcm.googleapis.com',
      password: 'myserverkey',
      port: 5235,
      host: 'gcm.googleapis.com',
      legacySSL: true,
      preferredSaslMechanism: 'PLAIN'
  };
  
  console.log('creating xmpp app');
  
  xmppClient = new xmpp.Client(options);
  
  xmppClient.connection.socket.setTimeout(0)
  xmppClient.connection.socket.setKeepAlive(true, 10000)
  
  redisClient = redis.createClient();
  redisClient.subscribe(redisSubChan);
  
  redisClient.on("message", function(channel, message) {
      console.log('received message');
      console.log(message);
      message = JSON.parse(message);
      //send the messages to google ccs server via xmpp
      var payload = {
          "to": message.to,
          "message_id": message.message_id,
          "data": message.data,
          "time_to_live": message.time_to_live,
          "delay_while_idle": message.delay_while_idle
      };
      var jsonPayload = JSON.stringify(payload);
      console.log(jsonPayload);
  
      var ackToDevice = new xmpp.Element('message', {'id': ''}).c('gcm', {xmlns: 'google:mobile:data'}).t(jsonPayload);
      console.log('prepared message');
      console.log(ackToDevice.root().toString());
      xmppClient.send(ackToDevice);
      console.log('sent!!!');
  });
  
  xmppClient.on('online', function() {
      console.log("online");
  });
  
  xmppClient.on('connection', function() {
      console.log('online');
  });
  
  xmppClient.on('stanza',
          function(stanza) {
              if (stanza.is('message') &amp;&amp; stanza.attrs.type !== 'error') {
                  // Best to ignore an error
                  console.log("Message received");
                  //Message format as per here: https://developer.android.com/google/gcm/ccs.html#upstream
                  var messageData = JSON.parse(stanza.getChildText("gcm"));
  
                  if (messageData &amp;&amp; messageData.message_type != "ack" &amp;&amp; messageData.message_type != "nack") {
  
                      var ackMsg = new xmpp.Element('message', {'id': ''}).c('gcm', {xmlns: 'google:mobile:data'}).t(JSON.stringify({
                          "to": messageData.from,
                          "message_id": messageData.message_id,
                          "message_type": "ack"
                      }));
                      //send back the ack.
                      xmppClient.send(ackMsg);
                      console.log("Sent ack");
                      //receive messages from ccs and give it to PHP workers
                      gearClient.submitJob(gearmanJobName, JSON.stringify(messageData), {background: true});
  
                  } else {
                      //Need to do something more here for a nack.
                      console.log("message was an ack or nack...discarding");
                  }
  
              } else {
                  console.log("error");
                  console.log(stanza);
              }
  
          });
  
  xmppClient.on('authenticate', function(opts, cb) {
      console.log('AUTH' + opts.jid + ' -&gt; ' + opts.password);
      cb(null);
  });
  
  xmppClient.on('error', function(e) {
      console.log("Error occured:");
      console.error(e);
      console.error(e.children);
  });
  </code></pre>
  
  <p>I am able to receive the messages from ccs server but <strong>can not</strong> send the downstream message from redis on message callback. </p>
  
  <p><strong>I get the following error</strong></p>
  
  <pre><code>error
  { name: 'message',
    parent: null,
    attrs: 
     { id: '',
       type: 'error',
       to: '1026645507924@gcm.googleapis.com/8DF23ED7',
       'xmlns:stream': 'http://etherx.jabber.org/streams' },
    children: 
     [ { name: 'gcm',
         parent: [Circular],
         attrs: [Object],
         children: [Object] },
       { name: 'error',
         parent: [Circular],
         attrs: [Object],
         children: [Object] } ] }
  </code></pre>
  
  <p>I tried to print(followed <a href="https://github.com/node-xmpp/node-xmpp/issues/233">node xmpp</a>) the xmpp stanza before sending and its same</p>
  
  <blockquote>
    <p>//log of my message</p>
  </blockquote>
  
  <pre><code>&lt;message id=""&gt;&lt;gcm xmlns="google:mobile:data"&gt;{"to":"APA91bHIGZcbePZ-f1jSyWQkBAJMHorHJiwgtN1GWITzcHf6uyVOZ3k7AasUiB-vBix32ucSypin3xDTYmoxqSc_ZmTDTuKjuDQ8BPQLpC41SqYRimm-hn34ZjSAF4uQO0OP1LSbqUxjh2WF0K5n4KyD3-Vn8ghASQ","message_id":84,"data":{"test":"sample data to send"},"time_to_live":0,"delay_while_idle":false}&lt;/gcm&gt;&lt;/message&gt;
  </code></pre>
  
  <p>as they mentioned in documentation(<a href="https://developer.android.com/google/gcm/ccs.html">Request format</a>). What is wrong with my code ?</p>