Skip to content

Instantly share code, notes, and snippets.

@malisetti
Created October 5, 2014 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save malisetti/06e685dae963c172519d to your computer and use it in GitHub Desktop.
Save malisetti/06e685dae963c172519d to your computer and use it in GitHub Desktop.
Google ccs server connection
var config = require('./gcmConfig');
var xmpp = require('node-xmpp');
var jobPayload;
var xmppClient;
var options = {
type: 'client',
jid: config.jid,
password: config.password,
port: config.port,
host: config.host,
legacySSL: true,
preferredSaslMechanism: 'PLAIN'
};
xmppClient = new xmpp.Client(options);
//xmppClient.connection.socket.setTimeout(0);
//xmppClient.connection.socket.setKeepAlive(true, 10000);
xmppClient.on('online', function() {
console.log("online");
});
xmppClient.on('connection', function() {
console.log('online');
});
xmppClient.on('stanza',
function(stanza) {
if (stanza.is('message') && stanza.attrs.type !== 'error') {
// Best to ignore an error
//Message format as per here: https://developer.android.com/google/gcm/ccs.html#upstream
var messageData = JSON.parse(stanza.getChildText("gcm"));
messageData.channel = redisSubChan;
var messageType = messageData.message_type;
if (messageType != "ack" && messageType != "nack" && messageType != "receipt" && messageType != "control") {
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);
}
//use the message
} else {
console.log("stanza error");
}
console.log(stanza.toString());
});
xmppClient.on('authenticate', function(opts, cb) {
console.log('AUTH' + opts.jid + ' -> ' + opts.password);
cb(null);
});
xmppClient.on('close', function(){
setTimeout(function(){
process.exit(0);
}, 1000);
});
xmppClient.on('disconnect', function(){
setTimeout(function(){
process.exit(0);
}, 1000);
});
xmppClient.on('end', function(){
setTimeout(function(){
process.exit(0);
}, 1000);
});
xmppClient.on('error', function(e) {
console.log("Error occured:");
console.error(e);
console.error(e.children);
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment