pubnub / swift

PubNub native Swift SDK for iOS, MacOS, WatchOS, TvOS
Other
31 stars 28 forks source link

didReceiveSubscription is not getting called #169

Closed mohitordex closed 6 months ago

mohitordex commented 7 months ago

Hey there,

I recently updated PubNub from version 6.0.5 to 7.1.0, but I've hit a snag: didReceiveSubscription isn't being called anymore.

I've double-checked, and PubNub is connected, and the channel is subscribed. Oddly enough, when I downgraded back to 6.0.5, everything worked fine - didReceiveSubscription was being called as expected.

Has anyone else encountered this issue after updating to 7.1.0? I'd really appreciate any guidance or insight you can offer. Thanks a bunch!

Screenshot 2024-04-16 at 4 09 40 PM

mohitordex commented 7 months ago

Screenshot 2024-04-16 at 5 31 43 PM

When I delve into debugging, I've noticed that although I successfully retrieve the message, the subsequent execution of didReceiveSubscription fails to trigger.

jguz-pubnub commented 7 months ago

Hi @mohitordex, could you show how you configured your listener?

mohitordex commented 7 months ago

@jguz-pubnub, Below code is working fine in v6.0.5 but not working in v7.1.0

// MARK: - PubNub Setup Methods
extension AdminVC {
    func setUpPubnub() {
        let defaults = UserDefaults.standard
        let strOrgID: String = defaults.string(forKey: "OrgId") ?? ""
        strChnlName = "\(appDelegate?.channelName ?? "")\(strOrgID)"
        Log.printLog(sender: self, logType: .pubNub, logValue: "Channel Name: \(strChnlName as Any)")
        strGlobalChnlName = appDelegate?.languageChannel ?? ""
        listener = SubscriptionListener(queue: .main)
        listener.didReceiveSubscription = { event in
            switch event {
            case let .messageReceived(message):
                Log.printLog(sender: self, logType: .pubNub, logValue: "Message Received: \(message) Publisher: \(message.publisher ?? "defaultUUID")")
                Log.printLog(sender: self, logType: .pubNub, logValue: "From PubNub")
                self.isDeleteLanguagePusher = false
                let subscription: String = message.subscription ?? ""
                Log.printLog(sender: self, logType: .pubNub, logValue: "Subscription :- \(subscription)")
                Log.printLog(sender: self, logType: .pubNub, logValue: "\(message.publisher ?? "") sent message to '\(message.channel)' at \(message.published) : \(message.payload)")
                // End
                Log.printLog(sender: self, logType: .pubNub, logValue: "[Received Message]: \(message)")
                let msgData = message.payload.jsonData
                do {
                    //  Convert Message data to dictionary
                    if let json = try JSONSerialization.jsonObject(with: msgData!, options: .mutableContainers) as? [String: Any] {
                        var orgChannel: String?
                        if let value = UserDefaults.standard.value(forKey: "OrgId") {
                            orgChannel = "\(self.appDelegate?.channelName ?? "")\(value)"
                        }
                        if orgChannel == subscription {
                            let strOperation = json["op"] as? String
                            Log.printLog(sender: self, logType: .pubNub, logValue: "Operation Name: \(strOperation as Any)")
                            if strOperation == PUB.WaitList.Add {
                                self.fromPubNub = true
                                guard let serviceResult = self.responseModel?.serviceResult else {  return }
                                guard let recordCounts = serviceResult.records?.count else {  return }
                                if recordCounts < self.currentPageSize {
                                    App.createSpinerFooter(tableView: self.tableViewWalkIn)
                                    self.fetchGuestList()
                                    //                             sleep(1)
                                    self.loadTimeMetrics()
                                }
                                self.fromPubNub = false
                            }
                        } else {
                            Log.printLog(sender: self, logType: .pubNubError, logValue: "Opration Name NOT LISTED")
                        }
                        Log.printLog(sender: self, logType: .pubNub, logValue: "Collected Data: \(json as Any)")
                    } else {
                        Log.printLog(sender: self, logType: .pubNubError, logValue: "Issue in listening from channel.")
                    }
                } catch {
                    Log.printLog(sender: self, logType: .pubNubError, logValue: error.localizedDescription)
                }
            case let .connectionStatusChanged(status):
                Log.printLog(sender: self, logType: .pubNub, logValue: "Status Received: \(status)")
            case let .presenceChanged(presence):
                Log.printLog(sender: self, logType: .pubNub, logValue: "Presence Received: \(presence)")
            case let .subscribeError(error):
                Log.printLog(sender: self, logType: .pubNub, logValue: "Subscription Error \(error)")
            default:
                break
            }
        }
        pubNub?.add(listener)
    }
}
mohitordex commented 7 months ago

@jguz-pubnub, Can you please tell me what am i missing or doing anything wrong ?

jguz-pubnub commented 7 months ago

@mohitordex, did you forget to call pubnub.subscribe(...)?

Here's the minimum example code I wrote to check listener.didReceiveSubscription closure and it works as expected. Can you verify it with the newest SDK (7.2.0) version?

import UIKit
import PubNub

class ViewController: UIViewController {
  var pubnub: PubNub!
  var listener: SubscriptionListener!

  override func viewDidLoad() {
    super.viewDidLoad()

    pubnub = PubNub(configuration: PubNubConfiguration(
      publishKey: "YourPublishKey",
      subscribeKey: "YourSubscribeKey",
      userId: "YourUserId"
    ))

    listener = SubscriptionListener()
    listener.didReceiveSubscription = { subscription in
      debugPrint(subscription)
    }

    pubnub.add(listener)
    pubnub.subscribe(to: ["test-channel"])
  }
}
mohitordex commented 7 months ago

Hey @jguz-pubnub,

I've encountered an issue with my app's functionality. Previously, upon user login, I would subscribe to channels using PubNub. Once connected and subscribed, I'd smoothly transition the user to a second view controller, passing the PubNub variable along. This setup worked seamlessly up to version 6.3.0.

However, upon updating to versions greater than 7.0.0, up to v7.2.0, I noticed this setup no longer functioned as expected. While the PubNub variable seemed fine on the second view controller – connected and subscribed – the didReceiveSubscription method failed to trigger.

Curiously, when I tested the above code you have given in a dummy project with PubNub version 7.2.0, it worked flawlessly. So, I experimented and found a workaround: after transitioning to the second view controller, I unsubscribed from the channels and then immediately resubscribed after adding a listener to PubNub on the second view controller. This fixed the issue.

My concern now is whether I need to implement this workaround for all my view controllers, or if there's a more elegant solution you could suggest.

I truly appreciate your assistance and time.

jguz-pubnub commented 7 months ago

Hey @mohitordex, I reproduced your scenario and indeed it's a bug. I'll work on that and let you know about the new version that fixes this issue.

mohitordex commented 7 months ago

Hello @jguz-pubnub , Could you please provide a rough estimate for when it will be released? We have a dependency on PubNub.

jguz-pubnub commented 6 months ago

Hi @mohitordex, we're planning to release a new version this week

jguz-pubnub commented 6 months ago

Released in 7.2.1