Skip to content

Instantly share code, notes, and snippets.

@jsancho
Last active April 17, 2018 21:37
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 jsancho/c472f6849f803236d25acbb4ab4f0429 to your computer and use it in GitHub Desktop.
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
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