Last active
April 17, 2018 21:37
-
-
Save jsancho/c472f6849f803236d25acbb4ab4f0429 to your computer and use it in GitHub Desktop.
Example of how to post a message to a fanout.io channel with an AWS lambda
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
exports.handler = (event, context, callback) => { | |
console.log('sending message to subscribers'); | |
console.log(JSON.stringify(event)); | |
console.log('using GRIP_URL'); | |
console.log(process.env.GRIP_URL); | |
const grip = require('grip'); | |
const faas_grip = require('faas-grip'); | |
// faas_grip.publish('mychannel', new grip.HttpStreamFormat( | |
// 'content: "data": {"text": "one more time!"}\n\n') | |
// ); | |
// const response = { | |
// statusCode: 200, | |
// body: JSON.stringify('message sent') | |
// }; | |
// setTimeout( | |
// function() { | |
// callback(null, response); | |
// }, 2000 | |
// ); | |
const source = JSON.parse(event.body); | |
const body = { | |
items: [ | |
{ | |
channel: `${process.env.FANOUT_CHANNEL}`, | |
formats: { | |
"http-stream": { | |
"content": `data: {"message":"${source.message}"}\n\n` | |
} | |
} | |
} | |
] | |
}; | |
const options = { | |
protocol: 'https:', | |
auth: `${process.env.FANOUT_REALM}:${process.env.FANOUT_KEY}`, | |
headers: { | |
'content-type': 'application/json', | |
}, | |
body: JSON.stringify(body) | |
}; | |
const got = require('got'); | |
got(`https://api.fanout.io/realm/${process.env.FANOUT_REALM}/publish/`, options) | |
.then(() => { | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('message sent') | |
}; | |
callback(null, response); | |
}) | |
.catch(err => { | |
callback(err); | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment