Last active
December 14, 2015 12:35
-
-
Save jparreira/3577179e9fd7292b5368 to your computer and use it in GitHub Desktop.
AWS Lambda function that send a message through the Realtime Messaging Platform
This file contains 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 https = require('https'); | |
exports.handler = function(event, context) { | |
var appkey = 'YOUR_REALTIME_APPKEY'; | |
var privatekey = 'YOUR_REALTIME_PRIVATEKEY'; | |
var channel = 'aws-iot'; | |
var message = JSON.stringify(event); | |
var postBody = "AK=" + appkey + "&PK=" + privatekey + "&C=" + channel + "&M=" + message; | |
var headers = { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': postBody.length | |
}; | |
var options = { | |
hostname: 'ortc-developers2-useast1-s0001.realtime.co', | |
port: 443, | |
path: '/send', | |
method: 'POST', | |
headers: headers | |
}; | |
var req = https.request(options, function(res) { | |
var body = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function(chunk) { | |
body += chunk; | |
}); | |
res.on('end', function() { | |
if(res.statusCode==201) { | |
console.log('Message sent successfully to Realtime'); | |
} else { | |
console.log('Error sending message to Realtime. Description: ', | |
body); | |
} | |
context.succeed(body); | |
}); | |
}); | |
req.on('error', context.fail); | |
req.write(postBody); | |
req.end(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment