Do I have to consume the message separately with SQS? Publishing my message with SNS and consuming with SQS feels weird to me.
This is the pubsub implementation
export const awsSNSPlugin = fp(async server => {
const snsService = new SNS({ apiVersion: '2010-03-31' });
const publish: AWSPubSub['publish'] = async params => {
try {
const response = await snsService.publish(params).promise();
server.log.info(`Message ${params.Message} sent to the topic ${params.TopicArn}`);
server.log.info(response);
} catch (err: any) {
server.log.error(err, err.stack);
}
};
const subscribe: AWSPubSub['subscribe'] = async (params, callback) => {
try {
const response = await snsService.subscribe(params).promise();
server.log.info(`this is subscribe response: ${JSON.stringify(response)}`);
} catch (err: any) {
server.log.error(err, err.stack);
}
};
server.decorate('pubSub', {
publish,
subscribe,
});
});
I will call the method publish to publish a message like "test message", which will output "Message test message sent to the topic testTopic" on successful publish. I expect the subscriber to then respond with "this is subscribe response: test message" , but it doesn't. It only fires when the subscriber is first initialized on server start with this message:
this is subscribe response: {"ResponseMetadata":{"RequestId":"e4e6b056-f388-5f3d-8a8b-eb884551eaa1"},
"SubscriptionArn":"arn:aws:sns:us-east-1:111111111111111:profileCreated:12hello34-3hi4-1234-hello-12b12bff1212"}
I can see that SNS topic is publishing to the SQS because the number of message goes up in the SQS management console. However, the SNS subscription function is not consuming the messages. It outputs the console log (the callback function) when the server is booted up, but it does not respond to any of the published messages.
CodePudding user response:
I think that you are asking how the Subscribe functionality works with an Amazon SNS topic.
Think of an Amazon SNS topic as a mailing list. Any message sent to the Topic will be forwarded to everything that has subscribed to the topic. If there are 10 subscribers, then then sending one message to the Topic will cause the message to be forwarded to all 10 subscribers.
The 'subscribe' action registers a recipient, such as an email address, SMS phone number, Amazon SQS queue or AWS Lambda function. Then, when a message is sent to the topic, that recipient receives the message.
You say:
I will call the method publish to publish a message like "test message", which will output "Message test message sent to the topic testTopic" on successful publish. I expect the subscriber to then respond with "this is subscribe response: test message" , but it doesn't. It only fires when the subscriber is first initialized on server start with this message...
I'm not sure what you mean by "expect the subscriber to respond" -- the act of subscribing is done only once, to register the recipient. Then, each message will be delivered to the recipient (eg SMS phone message or HTTP endpoint). So, it depends on what you registered as the recipient as to whether you would expect a "response".
Unfortunately, the code in your Question does not show any Subscribe parameters, so it is difficult to say what the expected behaviour should be.