GetStream / stream-chat-android

:speech_balloon: Android Chat SDK ➜ Stream Chat API. UI component libraries for chat apps. Kotlin & Jetpack Compose messaging SDK for Android chat
https://getstream.io/chat/sdk/android/
Other
1.45k stars 272 forks source link

CAS-142: Need an idiomatic way to subscribe to new messages in all available chats. #462

Closed AlexWih closed 4 years ago

AlexWih commented 4 years ago

I want to be notified when new message to any chat (which user participates in) is arrived. I have learned that I can do something like:

val client = StreamChat.getInstance(applicationContext)
client.addEventHandler(object : ChatEventHandler() {
    override fun onMessageNew(channel: Channel, event: Event) {
        super.onMessageNew(channel, event)
        // new message arrived
    }

    override fun onNotificationMessageNew(channel: Channel, event: Event) {
        super.onNotificationMessageNew(channel, event)
        Log.d("+++", "onNotificationMessageNew()")
        // new message arrived
    }
})

But it seems there is no events arriving if I don't execute client.queryChannels() before. Is there a more idiomatic way to subscribe to new messages without precautions?

elevenetc commented 4 years ago

To start getting channel events (like new message) you need to watch it. And query channel request with watch = true actually makes it.

What exactly you're trying to implement?

AlexWih commented 4 years ago

@elevenetc Thank you for quick response! We have our own implementation to show Android notifications and I want to show notification every time new messages arrives using this (our) implementation.

Do you mean I need to do it in two steps:

  1. Fetch all channels.
  2. Call watch() on every channel?
elevenetc commented 4 years ago

@AlexWih You're welcome. watch is better to use for screen where user is chatting and needs to get all events of a particular channel. Calling it on list of channels for every channel would work, but there're better ways.

So there're 3 ways to know that new message is arrived:

  1. connect socket (call setUser), call watch on a channel and receive message.new event. Catch it inside onMessageNew.
  2. connect socket (call setUser) and get notification.message_new event. Catch it inside onNotificationMessageNew.
  3. disconnect socket and receive firebase push message.

All these events are coming to members of a channel.

So my guess is that you're not getting any events because setUser either not called or not finished. onNotificationMessageNew should be called when new message is arrived, but it's not. Could you check logcat logs? Want to make sure that socket is connected.

AlexWih commented 4 years ago

@elevenetc The 2nd option is exactly what I am doing and setUser() is definitely called. But it doesn't work by default with this code:

val client = StreamChat.getInstance(applicationContext)
client.addEventHandler(object : ChatEventHandler() {
    override fun onMessageNew(channel: Channel, event: Event) {
        super.onMessageNew(channel, event)
        // do something
    }

    override fun onNotificationMessageNew(channel: Channel, event: Event) {
        super.onNotificationMessageNew(channel, event)
        // do something
    }
})

But it does work if I recursively query the all available channels using this approach:

private fun queryChannels(offset: Int = 0) {
    client.queryChannels(
        QueryChannelsRequest()
            .withLimit(Int.MAX_VALUE)
            .withOffset(offset)
            .withWatch()
            .withPresence(),
        object : QueryChannelListCallback {
            override fun onSuccess(response: QueryChannelsResponse) {
                val channelsCount = response.channels.size
                if (channelsCount > 0) {
                    queryChannels(offset + channelsCount)
                }
            }

            override fun onError(errMsg: String, errCode: Int) {
                // ....
            }
        })
}

So, the final working code looks like this:

val client = StreamChat.getInstance(applicationContext)
queryChannels()
client.addEventHandler(object : ChatEventHandler() {
    override fun onMessageNew(channel: Channel, event: Event) {
        super.onMessageNew(channel, event)
        // do something
    }

    override fun onNotificationMessageNew(channel: Channel, event: Event) {
        super.onNotificationMessageNew(channel, event)
        // do something
    }
})

So, my issue not that I am not getting events, but that there is no elegant way to receive new events without fetching the list of all available channels.

elevenetc commented 4 years ago

Unfortunately this is a limitation of SDK 3.x.x. Internally client maintains in-memory cache and tries to sync data (channels and messages). Client syncs data (and distributes events) only for channels which were requested before, so if channel was not requested events are ignored. That was done mainly because SDK was supposed to be used with ViewModels from SDK, but if client used separately then such kind of problems do appear.

There is still an option to get all events. ChatEventHandler has method handleEventFromUnregisteredChannel which receives events for channels which were not queried before:

client.addEventHandler(object : ChatEventHandler() {
    override fun onMessageNew(channel: Channel, event: Event) {
        //called if channel was received (queried) before
        handleEvent(event)
    }

    override fun onNotificationMessageNew(channel: Channel, event: Event) {
        //called if channel was received (queried) before
        handleEvent(event)
    }

    override fun handleEventFromUnregisteredChannel(channel: Channel, event: Event) {
        //called if channel was not received (queried) before
        handleEvent(event)
    }
})

There is no such limitation in the next version of SDK (4.x.x), although it's still not stable. If you're not using SDK UI components and ViewModels it might be convenient to use only client: https://github.com/GetStream/stream-chat-android-client/

AlexWih commented 4 years ago

@elevenetc Strange, I have checked callback handleEventFromUnregisteredChannel() and it seems it isn't invoked when I don't do client.queryChannels() and new messages comes

elevenetc commented 4 years ago

You're right, just tested it. handleEventFromUnregisteredChannel is not called for notification.message_new. When notification.message_new is received internally channel is queried actually for this particular event. So it still shouldn't be required to query channel.

Could you check if you're getting actually notification.message_new?

This flow should work, just tested it.

  1. Add listener ChatEventHandler .onNotificationMessageNew
  2. Call setUser
  3. Send message from another client (web, mobile or cli)
  4. In the logs you should see notification.message_new
  5. Then query channel request
  6. And finally onNotificationMessageNew should be called.

Let me know which step is failed.

AlexWih commented 4 years ago

@elevenetc Yes, currently I use this approach:

  1. Call setUser().
  2. Call client.queryChannels() for all channels.
  3. Add listener ChatEventHandler.onNotificationMessageNew() and ChatEventHandler.onMessageNew().
  4. Send message from another client.

And it works - ChatEventHandler.onMessageNew() is called, but the issue is the same I mentioned above: there is no way (at least I haven't found it) how to query all channels at once. Instead I have to do some kind of paginations which sometimes ends up with Too many requests issue.

Here is the code for step (2):

var counter: Int = 0

private fun queryChannels(offset: Int) {
    logger.log("Querying channels with offset $offset")
    client.queryChannels(
        QueryChannelsRequest(
            filterObject,
            querySort
        ).withLimit(Int.MAX_VALUE).withOffset(offset),
        object : QueryChannelListCallback {
            override fun onSuccess(response: QueryChannelsResponse) {
                val channelsCount = response.channels.size
                response.channels.forEach {
                    logger.log("Fetched channel: ${it.name}")
                }
                counter += channelsCount
                logger.log("Fetched this amount of channels: $channelsCount, total channels count: ${counter}")
                if (channelsCount > 0) {
                    queryChannels(offset + channelsCount)
                }
            }

            override fun onError(errMsg: String, errCode: Int) {
                logger.logException(message = "Failed to query channels, errMsg: ${errMsg}, errCode: $errCode, cache size: ${counter}")
            }
        })
}

As you can see from the logs the offset doesn't work as expected - I receive first 4 channels and then with offset 4 I receive the same 4 channels + 1 new. Shouldn't I receive new page with 4 new channels? It also leads to a situation that if there are lots of channels - the last pagination requests fails with "too many requests" error.

Here is the full log:

2020-05-12 12:14:25.751 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 0
2020-05-12 12:14:27.688 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Long night2
2020-05-12 12:14:27.688 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Long night
2020-05-12 12:14:27.688 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Balcony Concert
2020-05-12 12:14:27.688 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Who am I
2020-05-12 12:14:27.688 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 4, total channels count: 4
2020-05-12 12:14:27.688 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 4
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Long night2
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Long night
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Balcony Concert
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Who am I
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 5, total channels count: 9
2020-05-12 12:14:28.059 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 9
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Long night2
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Long night
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Balcony Concert
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Who am I
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 5, total channels count: 14
2020-05-12 12:14:28.327 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 14
2020-05-12 12:14:28.536 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Balcony Concert
2020-05-12 12:14:28.536 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Who am I
2020-05-12 12:14:28.536 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.536 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.536 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 4, total channels count: 18
2020-05-12 12:14:28.536 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 18
2020-05-12 12:14:28.767 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Balcony Concert
2020-05-12 12:14:28.768 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Who am I
2020-05-12 12:14:28.768 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.768 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.768 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Бдф
2020-05-12 12:14:28.769 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 7, total channels count: 25
2020-05-12 12:14:28.769 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 25
2020-05-12 12:14:28.976 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.976 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:28.976 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Бдф
2020-05-12 12:14:28.976 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 5, total channels count: 30
2020-05-12 12:14:28.976 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 30
2020-05-12 12:14:29.173 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:29.173 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:29.173 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Бдф
2020-05-12 12:14:29.173 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 5, total channels count: 35
2020-05-12 12:14:29.173 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 35
2020-05-12 12:14:29.574 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:29.574 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Бдф
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 100
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 99
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 98
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 97
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 96
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 9, total channels count: 44
2020-05-12 12:14:29.575 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 44
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Бдф
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 100
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 99
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 98
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 97
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 96
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 95
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 94
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 93
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 92
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 91
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 90
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 89
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 88
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 87
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 17, total channels count: 61
2020-05-12 12:14:30.069 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 61
2020-05-12 12:14:30.465 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 99
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 98
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 97
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 96
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 95
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 94
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 93
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 92
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 91
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 90
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 89
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 88
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 87
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 86
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 85
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 84
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 83
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 82
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 81
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 80
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 79
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 78
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 77
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 76
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 75
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 74
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 73
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 72
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 71
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 70
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 30, total channels count: 91
2020-05-12 12:14:30.466 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 91
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 69
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 68
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 67
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 66
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 65
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 64
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 63
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 62
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 61
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 60
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 59
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 58
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 57
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 56
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 55
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 54
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 53
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 52
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 51
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 50
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 49
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 48
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 47
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 46
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 45
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 44
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 43
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 42
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 41
2020-05-12 12:14:30.736 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 40
2020-05-12 12:14:30.737 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 30, total channels count: 121
2020-05-12 12:14:30.737 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 121
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 39
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 38
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 37
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 36
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 35
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 34
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 33
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 32
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 31
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 30
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 29
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 28
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 27
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 26
2020-05-12 12:14:31.019 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 25
2020-05-12 12:14:31.020 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 24
2020-05-12 12:14:31.020 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 23
2020-05-12 12:14:31.020 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 22
2020-05-12 12:14:31.020 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 21
2020-05-12 12:14:31.020 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 20
2020-05-12 12:14:31.020 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 19
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 18
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 17
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 16
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 15
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 14
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 13
2020-05-12 12:14:31.021 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 12
2020-05-12 12:14:31.022 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 11
2020-05-12 12:14:31.022 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 10
2020-05-12 12:14:31.022 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 30, total channels count: 151
2020-05-12 12:14:31.022 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 151
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 9
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 8
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 7
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 6
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 5
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 4
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 3
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 2
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: VIDEO 1
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 9, total channels count: 160
2020-05-12 12:14:31.226 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 160
2020-05-12 12:14:31.380 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:31.381 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 1, total channels count: 161
2020-05-12 12:14:31.381 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 161
2020-05-12 12:14:31.541 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:31.541 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 1, total channels count: 162
2020-05-12 12:14:31.541 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 162
2020-05-12 12:14:31.700 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:31.700 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 1, total channels count: 163
2020-05-12 12:14:31.700 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 163
2020-05-12 12:14:31.859 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched channel: Rggrg’s video room
2020-05-12 12:14:31.859 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Fetched this amount of channels: 1, total channels count: 164
2020-05-12 12:14:31.859 8430-8430/com.trember.myapplication D/StreamSocketWrapperImpl: Querying channels with offset 164
2020-05-12 12:14:32.009 8430-8430/com.trember.myapplication E/StreamSocketWrapperImpl: Failed to query channels, errMsg: QueryChannels failed with error: "Too many requests for user: b817f813-7efb-11ea-9835-0a2d336a3125, check response headers for more information.", errCode: 9, cache size: 164

So, initially I wanted just to receive notification over WS about new messages, but it turns out that there are lots of things need to do for this. It would be nice to have a single request which allows to query all the channels and avoid implementing this weird error-prone pagination.

elevenetc commented 4 years ago

initially I wanted just to receive notification over WS about new messages

But did you try what I've proposed? If you need to get notifications about new messages there is no need to query all channels. Client internally is doing everything for you ( without requesting all channels ). 6 steps which I mentioned do not have requirement to query all channels, yet it allows to get new messages events.

Or you need to query all channels for something else?

AlexWih commented 4 years ago

@elevenetc

  1. I did actually pretty the same what you proposed. Just in slightly different order - setUser() before setting ChatEventHandler, but also I tried the order you proposed - result is the same. May be I misunderstood something?
  2. steps which I mentioned do not have requirement to query all channels, yet it allows to get new messages events - actually not clear. If I don't query channel X - I am not receiving any updates on this X channel, i.e. neither onMessageNew() nor onNotificationMessageNew() nor handleEventFromUnregisteredChannel() are invoked.

If it is possible to receive those callbacks without querying all the channels - please let me know how.

elevenetc commented 4 years ago

2. I am not receiving any updates on this X channel, i.e. neither onMessageNew() nor onNotificationMessageNew() nor handleEventFromUnregisteredChannel() are invoked.

It should work. We need to debug and see what's in logs then. Could you go through all those 6 steps and check logcat?

  1. Add listener ChatEventHandler .onNotificationMessageNew
  2. Call setUser
  3. Send message from another client (web, mobile or cli)
  4. In the logs you should see notification.message_new
  5. Then query channel request
  6. And finally onNotificationMessageNew should be called.

4th and 5th are crucial and they are visible in logcat. Feel free to just drop all logs here, so I can help to find what happens there.

Let me know which step is failed.

AlexWih commented 4 years ago

@elevenetc For step 5 which exactly method I should invoke? Is it this one from Client.java - public void queryChannel(Channel channel, ChannelQueryRequest request, QueryChannelCallback callback) ?

elevenetc commented 4 years ago

Not you, it's done internally. That's indeed unclear from my message. You should just make first 3, all others are done internally by client.

AlexWih commented 4 years ago

@elevenetc Tried, here is the code which gets executed upon app start:

logger.log("Adding event handler...")
client = StreamChat.getInstance(applicationContext)
client.addEventHandler(object : ChatEventHandler() {
    override fun onMessageNew(channel: Channel, event: Event) {
        super.onMessageNew(channel, event)
        logger.log("onMessageNew(), unread count: ${channel.lastState?.currentUserUnreadMessageCount}, count2: ${channel.channelState?.currentUserUnreadMessageCount}")
    }

    override fun onNotificationMessageNew(channel: Channel, event: Event) {
        super.onNotificationMessageNew(channel, event)
        logger.log("onNotificationMessageNew()")
    }

    override fun onMessageRead(channel: Channel, event: Event) {
        super.onMessageRead(channel, event)
        logger.log("onMessageRead(), unread count: ${channel.lastState?.currentUserUnreadMessageCount}")
    }

    override fun handleEventFromUnregisteredChannel(client: Client?, event: Event) {
        super.handleEventFromUnregisteredChannel(client, event)
        logger.log("handleEventFromUnregisteredChannel(), event: ${event.message}")
    }
})

userProvider.currentUser.observeForever { user ->
    logger.log(
        "Setting user, previous user: ${client.user}, setting user ID: ${user!!.id.takeLast(4)}, setting token: ${user.token.takeLast(4)}"
    )
    client.setUser(
        User(user.id),
        user.token
    )
}

Here are the logs upon startup:

2020-05-12 17:37:06.473 14871-14871/com.trember.myapplication D/StreamSocketWrapperImpl: Adding event handler...
2020-05-12 17:37:06.478 14871-14871/com.trember.myapplication D/StreamSocketWrapperImpl: Setting user, previous user: null, setting user ID: 3125, setting token: GBxg

2020-05-12 17:37:07.768 14871-15002/com.trember.myapplication D/EchoWebSocketListener: WebSocket #1 Connected : Response{protocol=http/1.1, code=101, message=Switching Protocols, url=https://chat-us-east-1.stream-io-api.com/connect?json=%7B%22user_id%22%3A%22b817f813-7efb-11ea-9835-0a2d336a3125%22%2C%22user_details%22%3A%7B%22id%22%3A%22b817f813-7efb-11ea-9835-0a2d336a3125%22%7D%2C%22X-STREAM-CLIENT%22%3A%223.6.12-release%22%2C%22server_determines_connection_id%22%3Atrue%7D&api_key=audear3nqmss&authorization=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VyX2lkIjoiYjgxN2Y4MTMtN2VmYi0xMWVhLTk4MzUtMGEyZDMzNmEzMTI1In0.iav3U0aoIjWxbGd_HbnBOCx1wx7zZwQhyVgqVbnGBxg&stream-auth-type=jwt}
2020-05-12 17:37:07.770 14871-15002/com.trember.myapplication D/EchoWebSocketListener: WebSocket # 1 Response : {"type":"health.check","connection_id":"ee0ec247-afe1-4f3d-bdfd-ebcab22f72a0","cid":"*","me":{"id":"b817f813-7efb-11ea-9835-0a2d336a3125","role":"user","roles":[],"created_at":"2020-04-20T12:26:22.590984Z","updated_at":"2020-05-12T14:29:14.155229Z","last_active":"2020-05-12T14:37:07.693132433Z","banned":false,"online":true,"invisible":false,"devices":[{"push_provider":"firebase","id":"e9txkdphRcmbO1vcxkItOo:APA91bGAS1mrystJQdp-Fq5MHve9OTKVUQW5dEobNMxHSxoClgoPxmS2i9--9Qm2cAMEaGyg1qmTCcvN4UDUDIkgNvoyIFeROdUuTniWtRwwxrenCSo8TfKN5RvsOTUQFLsNFchhEyo4","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-08T20:09:00.846546Z"},{"push_provider":"firebase","id":"eZBKLpZZmC4:APA91bFtjmEb0trD0S0oRdrItXrPlLhRkSBQuaPcc4hcCyCErY60E3Ej4UgHrkZANvXwbWg47yRTh8ktvlT1RUsX48tWZKzk_9frcXsR-EVGIfLiA8Pe319aBARxM3YajAX9YeY2WCDE","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-07T15:37:24.781423Z"},{"push_provider":"firebase","id":"dApDKmzMRcyHr2G9SnHxad:APA91bHtCbSv3tXMtZ7sMgkGIQMFFC1Lu3CWsWGtj7fh5RbSk2z-GZJ7vIhbG_Cp8RmF6Gm1Qjgt38B4mSf0odUlhQf9vZ3LGBokkN4SA7WjVUZfOss0F6d1lwmdiuBTm-FPlWH67eaq","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-06T15:04:09.620722Z"},{"push_provider":"firebase","id":"chxqu2wJSYirBDb_POPqrK:APA91bG9m3Q9TlIh14_nfzUmiWLtrvWGEtHazRr0AiPoCgfXpdVwleVpOwtK8xKs8Em6ZSzJebExxh5RACvpFHNIQ6bknrmgue66dNoocETCJjN3S-5_nefeLaKSmdWgmjFaVfptaCCc","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-06T12:30:58.801816Z"},{"push_provider":"firebase","id":"eBB9N6tFTCq3gS7B7UEEa6:APA91bFAT_AoNtLOHDuofK2nwHsKBlvK9LcIzZLDRYvYaeUn6ixut3tE_bQgLKNlStAQzqxaq-sq3sZXiAlxtXm32l1iYh1-TP5_biCNqE-FXY98AYk2WcSW45BWLDHiVD7pfmBAANX3","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-06T12:16:31.782898Z"},{"push_provider":"firebase","id":"fnVZFrI5Ry-0Cf6ezwzCE0:APA91bGMU2O_XOtu8kd-jLy846wFretAk7Oe8THP_tVLjy1tdkZY9xHlfXCCoa0IL6L6FyF_zqPLte6YsVlcR33A9c-vhIiwk_r1cQhKhrIQDdZ7EHOil8fbnXk-2Q4TVoxXZeS60Hvz","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-05T16:39:40.845841Z"},{"push_provider":"firebase","id":"fuhWLtl_BdU:APA91bGGSGF36q8xj_LzuKX_V_cfisKbUmrse0XUSFcs7RVFEtl8SuLzb777xuDFF8rueaUkv8XijmPred8JN1FOVb2DARCSBD568SPwqY7n1IIbGGkLezbRcLC8Ym4AkKahLi8Jc2eC","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-05-05T09:23:08.910904Z"},{"push_provider":"firebase","id":"fwxxvUvbp8s:APA91bHWiDiOTbfyVHhQ212de3Q6ZJ8U6DgdJ-Bkf-iSMYb9MXXvUTW-u9WyLZ_RnonWaxQgRr1En5MjCazNP6FhOz3hfzMgD6EZTeVXpzjJ4GbWxqSBgpB56Ablluw6uWbeL5jpQ87f","user_id":"b817f813-7efb-11ea-9835-0a2d336a3125","created_at":"2020-04-24T00:26:49.190208Z"}],"mutes":[],"channel_mutes":[],"unread_count":24,"total_unread_count":24,"unread_channels":2,"image":"","name":"Rggrg"},"created_at":"2020-05-12T14:37:07.696485804Z"}
2020-05-12 17:37:07.976 14871-15002/com.trember.myapplication D/EchoWebSocketListener: Received event of type HEALTH_CHECK
2020-05-12 17:37:08.224 14871-15002/com.trember.myapplication D/EchoWebSocketListener: WebSocket # 1 Response : {"type":"health.check","connection_id":"ee0ec247-afe1-4f3d-bdfd-ebcab22f72a0","cid":"*","created_at":"2020-05-12T14:37:08.149830973Z"}
2020-05-12 17:37:08.224 14871-15002/com.trember.myapplication D/EchoWebSocketListener: Received event of type HEALTH_CHECK

When I send any message to any channel - nothing happens, there is no logs except HEALTH_CHECKs

elevenetc commented 4 years ago

The only reason why this might happen is when current user is not a member. That is a requirement for getting new messages events.

Could you check if current user is a member of the channel which you're trying get updates?

If user is not a member I'm afraid there is no way to get notification about new message right now without watching it explicitly.

tbarbugli commented 4 years ago

Just to summarize before closing this issue:

In your case you definetely want to add an event callback for the notification events (this is a client event).