In Scenario 2.4, we saw how multiple subscribers reading from the same SQS queue could ‘steal’ messages from each other, so that no single subscriber saw all the messages in the queue. Sometimes it is a requirement that multiple subscribers to a queue receive all messages, so some mechanism for publishing a single message to multiple queues is required. The solution is to use an intermediatry to ‘fan out’ the message to multiple subscribers. The intermediary in this case, is Amazon SNS, the Simple Notification Service. Amazon SNS allows an application to send a single message intent, and have that message delivered to one or more targets. The target(s) of the message can be an HTTP endpoint, a mobile device, or an SQS queue - and in fact, multiples and any combination of these. The architecture for this scenario looks like this:
Contents
Also note that the Messages Received statistic is 3 times the Messages Sent statistic, which is to be expected if the messages sent are ‘fanned out’ to three queues.
Adding one or more SQS queues as targets for SNS notifications is simple. Lets take a look at this now.
You will see three subscribers - one for each of the three SQS queues arn:aws:sqs:us-west-2:277844687642:idevelop_drawqueue_XX where XX is from 1
to 3
.
When a notification is sent to the SNS topic, it is automatically routed through to each of the subscribers, in this case, to three SQS queues. When the browser implementation sends draw points to the SNS topic, they too are routed through to three SQS queues, which are each subscribed to by the web page implementation, and the points rendered on the three subscriber canvases.
This snippet of Javascript shows how messages are sent to the SNS topic:
var params = {
Message: JSON.stringify(point),
TopicArn: "<SNS Topic ARN>"
};
snsInstance.publish(params, function(err, data){});
Where can I find more information about the AWS SDK for Javascript and SNS?
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html