guy_m
guy_m

Reputation: 3307

AWS-SNS and Node.js: subscribe multiple Endpoints (ARNs) to a Topic

I'm building a chat application for Android using a Node.js server..
When a user wants to open a new Topic/chat group, few things need to be done:
1. Create the Topic using Amazon's (AWS) SNS SDK.
2. Subscribe each one of the requested users to this Topic, using their AWS Endpoint ARN.

My question is on the Node.js AWS SDK, but corresponds to the AWS Android API as well as they currently work the same in this matter.

Question 1:
How do I subscribe multiple Endpoint ARNs in one call?
Currently I have to wait after each "subscription request" to make sure it is successful before I submit the next one. It takes too much time and effort.

Question 2:
In case a user wants to unsubscribe from the entire app, I delete his/her Endpoint from AWS-SNS.
BUT, their subscriptions to the various topics remain! So effectively, they still receive all the data/messages (and there is no app to receive it of course).

Please let me know if any code is needed.
* I thought maybe I would just put a json'd List with all the ARNs, but it did not work.

Upvotes: 2

Views: 1454

Answers (1)

Colin Wang
Colin Wang

Reputation: 6958

Currently AWS SDK does not accept the array of Arns.

AWS-SDK/sns.subscribe

The possible solution is to use a bulk array of requests but this action is throttled at 100 transactions per second (TPS) according to the AWS documentation. Here is an example of Node.js API calls.

const subscribe = async (TopicArn, arns) => {
    try {
        const promises = []
        arns.forEach((Endpoint) => {
            promises.push(sns.subscribe({
                Protocol: 'application',
                TopicArn,
                Endpoint,
            }).promise())
        })
        await Promise.all(promises)
    } catch (e) {
        console.error(e)
    }
}

Upvotes: 0

Related Questions