To many requests for an authToken. The maximum request allowed are 100 per minute. Please retry after 10 seconds

I am getting this error on com.cometchat:pro-android-chat-sdk:2.0.0-beta8

Hi @imayank91 , can you upgrade your android sdk to 2.0.1 and try again.

Hi @ darshan.bhanushali. I revved the version but It didn’t solve the issue, what I have noticed is that it happens with few users not all.

Also, I do the following for every message in the adapter . Is this incorrect?

if (baseMessage.readAt == 0L) {
CometChat.markAsRead(baseMessage.id, baseMessage.receiverUid, baseMessage.type)
}

I changed this to read the last message only but I still get the error and it is only happening for few users. I don’t understand what could be the cause because no way I am hitting the api 100 times in a minute.

if (baseMessage.readAt == 0L) {
CometChat.markAsRead(baseMessage.id, baseMessage.receiverUid, baseMessage.type)
}

Hi @imayank91,

Calling CometChat.markAsRead() in the adapter is a wrong way to mark messages as read. As you are calling this method inside the adapter, it will be called for each message with readAt = 0. That’s the reason you are getting The maximum request allowed are 100 per minute error.

The correct way to mark messages as read is to call CometChat.markAsRead() while adding messages in adapter i.e onTextMessageRecieved() and in onSuccess of fetchPrevious() and

I have made this change already and I still get this error though only for few users not everyone.

Hi @imayank91, Can you share the code snippet of blocks or methods where you are using markAsRead()?

I am just using it once when setting the list in adapter

    val recentMessage = userFilteredList.valueAt(userFilteredList.size() - 1)
    if (recentMessage.readAt == 0L) {
        CometChat.markAsRead(recentMessage.id, recentMessage.receiverUid, recentMessage.type)
    }

Hello @ imayank91
This issue is not related to the markAsRead feature. This issue is caused due to too many request hitting our servers from the same authToken.
I believe that you have implemented message fetching on scrolling. I would suggest you check the logic for the same as if that part has not been implemented correctly, this could cause multiple requests to be triggered.
Do let us know if you are still facing this issue. We will be happy to assist you with the same at any point of the time.

Hi @aditya.gokula I raised this issue earlier as well and I did send you the code of what I was doing on scroll listener.

            if (!recyclerView.canScrollVertically(-1)) {
                onetoOneViewModel.fetchMessage(LIMIT = 30, userId = userId)
                scrollFlag = false
            }

Please check this at your end.

Hi @imayank91,

Are you using any CometChat method CometChat.getUser(), CometChat.getGroup(), etc. in a loop?

Hi @ darshan.bhanushali

I just verified that I am not using them in a loop. Also, I am sure this is not happening for every user. But for one user even If I don’t use the app for a day and open it on the following day I get this error. Could this mean something?

Hello,
Can you please confirm if you are using any local caching mechanism at you end where you are maintaining data related to CometChat. Also, this error can only mean that there are more than 100 requests being triggered for a single logged in user. We have tried multiple scenarios at our end but are not able to replicate this issue. We would like some more input from your end to be able to get to the root cause of this issue. Any more information from your side like when this error is actually witnessed would be of great help. We regret the inconvenience caused to you and appreciate your patience but we will need your help to get to the root of this issue.

Thanks

Hi aditya.gokula
This happens for a particular user. Will it be helpful if I share the userid for the user and you can check it at your end then.

Hello @imayank91,
Yes, you can share the UID of the user and also your appId so that we can check this at our end.

Hello @imayank91
We tried with these details in our sample apps and everything worked as expected. Can you please check which method is triggering this in the app. I believe this is something you have not been able to replicate but is being faced by a user of your app…??
We would like some more information from your end especially which method of the CometChat SDK is causing this to happen.
Also, is it possible for me to download the live version of your app from the play store and log in and check this ??

So first I fetch all the messages using the below mentioned method to load up recent chat screen

@WorkerThread
fun fetchAllMessages(LIMIT: Int, callback: ChatResponseCallback) {

    try {
        if (messageRequest == null) {
            messageRequest = MessagesRequest.MessagesRequestBuilder().setLimit(LIMIT).build()
        }
        messageRequest!!.fetchPrevious(object :
            CometChat.CallbackListener<List<BaseMessage>>() {
            override fun onSuccess(p0: List<BaseMessage>?) {
                mutableCurrentSetCount.value = p0?.size
                p0?.let { mutableAllMessageList.addAll(it) }
                allMessageList.value = mutableAllMessageList
                callback.onSuccess(p0?.size!!)
                Log.d(TAG, "messageRequest onSuccess: ${p0?.size}")
            }

            override fun onError(p0: CometChatException?) {
                callback.onFailure()
                Log.d(TAG, "messageRequest onError: ${p0?.message}")
            }
        })


    } catch (e: NullPointerException) {
        e.printStackTrace()
    }

}

and then when press on a user item , I use below mentioned method to fetch messages

@WorkerThread
fun fetchMessage(LIMIT: Int, userId: String) {

    try {
        if (messageRequest == null) {
            messageRequest =
                MessagesRequest.MessagesRequestBuilder().setUID(userId).setLimit(LIMIT).build()
        }
        messageRequest?.fetchPrevious(object : CometChat.CallbackListener<List<BaseMessage>>() {
            override fun onSuccess(p0: List<BaseMessage>?) {
                if (p0 != null) {
                    for (baseMessage: BaseMessage in p0) {
                        if (!baseMessage.category.equals(
                                CometChatConstants.CATEGORY_ACTION,
                                ignoreCase = true
                            )
                        ) {
                            mutableAllMessageList.add(baseMessage)
                            mutableOneToOneMessageList.add(baseMessage)
                        }
                    }
                    onetoOneMessageList.value = mutableOneToOneMessageList
                    allMessageList.value = mutableAllMessageList

                }

                Log.d(TAG, "messageRequest onSuccess: ${p0?.size}")

            }

            override fun onError(p0: CometChatException?) {
                Log.d(TAG, "messageRequest onError: ${p0?.message}")

                Log.d(TAG, "messageRequest onError: ${p0?.stackTrace}")
            }
        })
    } catch (e: NullPointerException) {
        e.printStackTrace()
    }

}

Hi @imayank91,

You can use our Conversation feature instead of fetchAllMessages() to display list of conversation with user and group in your recent chat screen.

You can follow our Docs to implement conversation in you recent chat screen.

1 Like

I can’t use this method as it will require a lot of change on my side which I can’t accommodate for now.

Hi @imayank91,

The API server has request throttling configured as 100 requests per auth token per minute.
Creating recent chats view using message request builder is not an optimised approach.

If user A has 100 chat messages with user B at time frame T1 and 100 chat messages with user C at time frame T2 where T2>T1, in order to show recent chat list you will have to use message request builder with the limit of 30 messages more than 4 times to get 2 recent conversations. Also, with message request builder you will have to fetch all the messages builder generating the correct recent chat list. With this approach the number of API calls required to generate the recent chat list increases with the number of messages.

In order to resolve the issue you have mentioned with “Too many requests …” we recommend the use of conversations feature.

1 Like