Created
July 6, 2015 12:14
-
-
Save heavymery/641a8649b5013b699f43 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var xmpp = require('node-xmpp'); | |
//Set node-xmpp options. | |
//Replace with your projectID in the jid and your API key in the password | |
//The key settings for CCS are the last two to force SSL and Plain SASL auth. | |
var options = { | |
type: 'client', | |
jid: '[email protected]', | |
// jid: '[email protected]', | |
password: 'xxxx', | |
// The CCS XMPP endpoint runs at gcm.googleapis.com:5235. | |
// When testing functionality (with non-production users), you should instead connect to gcm-preprod.googleapis.com:5236 (note the different port) | |
port: 5235, | |
host: 'gcm.googleapis.com', | |
// Note that a connection receives upstream messages destined for its GCM sender ID, regardless of which environment (gcm or gcm-preprod) it is connected to. Therefore, test code connecting to gcm-preprod.googleapis.com:5236 should use a different GCM sender ID to avoid upstream messages from production traffic being sent over test connections. | |
// port: 5236, | |
// host: 'gcm-preprod.googleapis.com', | |
legacySSL: true, | |
// preferredSaslMechanism: 'PLAIN' | |
preferred: 'PLAIN' | |
// reconnect: true | |
}; | |
console.log('creating xmpp app'); | |
var client = new xmpp.Client(options); | |
client.connection.socket.setTimeout(0); | |
client.connection.socket.setKeepAlive(true, 10000); | |
// None of these callbacks are called | |
client.on('online', function() { | |
console.log('online'); | |
}); | |
client.on('connection', function() { | |
console.log('connection'); | |
}); | |
client.on('authenticate', function(opts, cb) { | |
console.log('authenticate'); | |
}); | |
client.on('close', function() { | |
console.log("close"); | |
}); | |
client.on('stanza', function(stanza) { | |
if (stanza.is('message') && | |
// Best to ignore an error | |
stanza.attrs.type !== '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 && messageData.message_type != "ack" && messageData.message_type != "nack") { | |
var ackMsg = new xmpp.Element('message').c('gcm', { xmlns: 'google:mobile:data' }).t(JSON.stringify({ | |
"to":messageData.from, | |
"message_id": messageData.message_id, | |
"message_type":"ack" | |
})); | |
//send back the ack. | |
client.send(ackMsg); | |
console.log("Sent ack"); | |
//Now do something useful here with the message | |
//e.g. awesomefunction(messageData); | |
//but let's just log it. | |
console.log(messageData); | |
} 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) | |
} | |
}); | |
client.on('error', function(e) { | |
console.log("error"); | |
console.error(e); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment