Skip to content

Instantly share code, notes, and snippets.

@anderson-marques
Last active May 1, 2019 20:10
Show Gist options
  • Save anderson-marques/09c661e4b2003c7f65b3b9ee6674757a to your computer and use it in GitHub Desktop.
Save anderson-marques/09c661e4b2003c7f65b3b9ee6674757a to your computer and use it in GitHub Desktop.
Fake SNS Module to be used in local laboratory - To real utilisation, replace the implementation to call the AWS SNS SDK
'use scrict'
\**
* https://repl.it/@marquesanderson/fake-sns
*\
const events = require('events')
const eventsEmitter = new events.EventEmitter()
class SNSPublisher {
constructor (topic) {
this.topic = topic
}
publish (message) {
console.log(`Publishing message=${JSON.stringify(message)} to topic: ${this.topic}`)
eventsEmitter.emit(this.topic, {
'Records': [
{
'EventVersion': '1.0',
'EventSubscriptionArn': 'arn:aws:sns:fake-sns',
'EventSource': 'aws:sns',
'Sns': {
'SignatureVersion': '1',
'Timestamp': '1970-01-01T00:00:00.000Z',
'Signature': 'fake-signature',
'SigningCertUrl': 'fake-cert-url',
'MessageId': '95df01b4-ee98-5cb9-9903-4c221d41eb5e',
'Message': JSON.stringify(message),
'MessageAttributes': { },
'Type': 'Notification',
'UnsubscribeUrl': 'fake-url',
'TopicArn': `arn:aws:sns:fake-sns:this.topic`,
'Subject': 'FakeInvoke'
}
}
]
})
}
}
class SNSSubscriber {
constructor (topic, callback) {
eventsEmitter.on(topic, (event) => {
callback(JSON.parse(event.Records[0].Sns.Message))
})
}
}
const producer = new SNSPublisher('some-topic')
const consumer1 = new SNSSubscriber('some-topic', (message) => {
console.log(`Consumer 1 - Message received: ${message}`)
})
const consumer2 = new SNSSubscriber('some-topic', (message) => {
console.log(`Consumer 2 - Message received: ${message}`)
})
producer.publish('Hello World')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment