winston
winston

Reputation: 3100

Can't receive PubNub message unless I publish a message first

In my Swift application, I instantiate PubNub on viewDidLoad with the correct configuration and then join a channel. However, it looks as if it doesn't join the channel at all unless I fire the Send button action to send a message (I threw the config/join channel code in the Send button just to see if it would work). The code in the Send button function is identical to the code in the viewDidLoad, but the code in viewDidLoad is still ignored. This is frustrating because the current user on my app won't receive messages sent to the channel unless the user first sends a message.

Here's the code in the view controller (I removed a lot of the code so it's easier to look at. If you think the issue lies elsewhere I can post more):

class MessageViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, PNObjectEventListener{

    var pubNub: PubNub!

    var pnConfiguration: PNConfiguration!

    var ids: String = ""

@IBAction func sendButton(sender: UIButton) {

        //create Pubnub channel
        pnConfiguration = PNConfiguration(publishKey: "key", subscribeKey: "key")
        pnConfiguration.uuid = currentUser.objectId!
        print(pnConfiguration.uuid)
        pubNub = PubNub.clientWithConfiguration(pnConfiguration)
        let channelName = ids as String
        let channelArray: [String] = [channelName]
        pubNub.subscribeToChannels(channelArray, withPresence: true)
        pubNub.publish(self.messageText.text!, toChannel: channelName, compressed: false, withCompletion: nil)
        pubNub.addListener(self)
        self.messageText.enabled = true
        self.view.userInteractionEnabled = true

        dispatch_async(dispatch_get_main_queue()) {
            self.messageText.text = ""
            self.messageTableView.reloadData()
        }
    }

override func viewDidLoad() {
        super.viewDidLoad()

        //Query that sets self.ids = user1.objectId! + user2.objectId!

        //set Pubnub config
        pnConfiguration = PNConfiguration(publishKey: "key", subscribeKey: "key")
        pubNub = PubNub.clientWithConfiguration(pnConfiguration)
        pnConfiguration.uuid = currentUser.objectId!
        //create channel
        let channelName = ids as String
        print(channelName)
        let channelArray: [String] = [channelName]
        pubNub.subscribeToChannels(channelArray, withPresence: true)
        pubNub.addListener(self)

        dispatch_async(dispatch_get_main_queue()) {
            self.messageTableView.reloadData()
        }
    }

EDIT:

After looking at the logs it looks like the reason why it's not working is because it's joining a channel called "", even though the print(channelName) returns the real channel name. Why isn't it taking the channel name? Is it not the right data type?

Upvotes: 0

Views: 469

Answers (1)

Serhii Mamontov
Serhii Mamontov

Reputation: 4932

In your code snippet I don't see any place where you set real value for ids variable which you declare at line #7. So client use value which is stored in that variable: ""
I think this variable changed during controller life-cycle and set to proper value before you hit sendButton but not available at time when view is loaded.

Best regards, Sergey

Upvotes: 1

Related Questions