Skip to content

Instantly share code, notes, and snippets.

@lance
Last active August 21, 2020 18:49
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 lance/d71dbb2c26855dcfc746ede10c89f168 to your computer and use it in GitHub Desktop.
Save lance/d71dbb2c26855dcfc746ede10c89f168 to your computer and use it in GitHub Desktop.
const { CloudEvent } = require('cloudevents');
const axios = require('axios');
// this could also be exported at the top level - shown this way to illustrate the namespace
const { HTTP } = require('cloudevents/messages');
// user-provided send function conforming to an interface
// each protocol may have it's own interface depending on
// the message structure determined by the spec. For HTTP
// this is, of course, the headers and body
function sender(headers, body) {
return axios.post('https://receiver.example.com', {
headers: headers,
data: body
});
}
// create a CloudEvent
const ce = new CloudEvent({
source: '/com.redhat.time.server',
type: 'com.redhat.time',
data: Date.now()
});
// create a timestamp message in binary format
const message = HTTP.asBinary(ce);
// send the message
HTTP.send(sender, message);
// This could all be inlined, of course
HTTP.send((headers, body) => {
return axios.post('https://receiver.example.com', {
headers: headers,
data: body
});
}, HTTP.asBinary(new CloudEvent({
source: '/com.redhat.time.server',
type: 'com.redhat.time',
data: Date.now()
})));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment