Skip to content

Instantly share code, notes, and snippets.

@mikalv
Forked from jamesvnz/gcmcss.js
Created November 7, 2016 16:06
Show Gist options
  • Save mikalv/61aa2c486cbd7fd9110c94ca6aca47b4 to your computer and use it in GitHub Desktop.
Save mikalv/61aa2c486cbd7fd9110c94ca6aca47b4 to your computer and use it in GitHub Desktop.
Sample node.js server code to implement an XMPP server that will integrate with Android's Google Cloud Messaging (GCM) "device to cloud" message functionality - CCS. This sample only receives upstream messages (i.e. from the device).
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]',
password: 'XXXXXXXX',
port: 5235,
host: 'gcm.googleapis.com',
legacySSL: true,
preferredSaslMechanism : 'PLAIN'
};
console.log('creating xmpp app');
var cl = new xmpp.Client(options);
cl.on('online',
function() {
console.log("online");
});
cl.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.
cl.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)
}
});
cl.on('error',
function(e) {
console.log("Error occured:");
console.error(e);
console.error(e.children);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment