2

My main task is to update Active Chat Member count on UI. I am calling pubnub.HereNow() when PresenceEventResult happens. Everything works fine, except case when my phone goes sleep and wake up again.

When my phone goes sleep, leave presence event is fired, I see it when I check console on my second client (this is probably due the checkbox "Generate Leave on TCP FIN or RST" is checked in Admin panel of PubNub). But when I turn on my phone, I still able to retrieve messages from other users, but if I check HereNow I see only that other user is presented. I tried to call subscribe one more time in Unity like this (I thought maybe it will fire event again, but it doesn't):

private void OnApplicationPause(bool pause)
{
    Debug.Log("OnApplicationPause(): " + pause);
    if (!pause)
    {
        pubnub.Subscribe()
            .Channels(new List<string>() {
            channel
            })
            .WithPresence()
            .Execute();
    }
}

Do you have any ideas, how can I notify pubnub servers that I am back?

Aleksandrs
  • 1,488
  • 1
  • 14
  • 34
  • 1
    Include as much code as you can with that support ticket: PubNub init with actual property values, sub-key, etc. I suspect that the "channel" var is null/empty string? But won't know until we see logs. Here's the SDK log enablement docs https://www.pubnub.com/docs/sdks/unity/troubleshooting – Craig Conover May 19 '21 at 16:04
  • 2
    Thank you for such kindness. I have raised ticket. No `channel` is not empty or null for sure. Interesting thing I found in logs with ENABLE_PUBNUB_LOGGING, if I understood correct, after it wakes up client leaves channel, and then HereNow shows only 1 client. And after it Subscribes again. But probably your experts will understand better :) – Aleksandrs May 20 '21 at 06:42
  • 1
    I think you could try and combine it with an additional [`OnApplicationFocus`](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationFocus.html) not sure if maybe one of the events gets "eaten" by the other in your case – derHugo May 20 '21 at 07:27
  • 2
    @derHugo, I put Presence code inside `OnApplicationPause` and it calls it successfully. Problem is, when PN reconnects automatically after mobile wakes up, it fires leave event. Interesting moment, that after exactly 4 minutes it calls join event. And HereNow returns 2 clients, as it expected by me. – Aleksandrs May 20 '21 at 07:58
  • I understand "the 4 minutes" scenario. Has to do with how PN subscribe works. Try adding code to your `status` callback that calls `pubnub.subscribe([channels])` when you get a `PNNetworkUpCategory` status event. You might have to cache the list of channels you are subscribed to in the client. JS SDK automates this a bit more. Read [Connection Management](PNNetworkUpCategory) to get more insights into what is happening here. – Craig Conover May 21 '21 at 18:07
  • I have tried `subscribe()` before I have made this post :) unsuccessful. After some logs investigation and searching through docs. I figured that `pubnub.Reconnect();` do the trick. Everything working for me. – Aleksandrs May 24 '21 at 05:11

1 Answers1

0

I am not sure if this 100% correct from PubNum side, but it's working for me:

private void OnApplicationPause(bool pause)
{
    if (!pause)
    {
        pubnub.Reconnect();
    }
}
Aleksandrs
  • 1,488
  • 1
  • 14
  • 34