3

It appears to me that the documentation PubNub has for getting started in Swift don't apply to versions earlier than PubNub 4.0. I can't successfully establish a callback to register with PubNub.

My code:

class Communicator: NSObject, PNObjectEventListener {

    var pubNubClient: PubNub

    override init(){
        let config = PNConfiguration(
            publishKey: "my_publish_key",
            subscribeKey: "my_subscribe_key"
        )
        pubNubClient = PubNub.clientWithConfiguration(config);
        super.init()
        pubNubClient.addListener(self)
        pubNubClient.subscribeToChannels(["my_channel"], withPresence: false)
    }

    func didReceiveMessage(client: PubNub!, message: PNMessageResult!){
        /* THIS METHOD NEVER GETS REACHED */
    }
}

Digging into the PubNub source a bit, this is the area that seems to be having problems:

- (void)addListener:(id <PNObjectEventListener>)listener {

    dispatch_async(self.resourceAccessQueue, ^{

        if ([listener respondsToSelector:@selector(client:didReceiveMessage:)]) {
            /* this block is never reached!!! */
            [self.messageListeners addObject:listener];
        }

    /* Remaining Lines Stripped Away */
    });
}

I'm still relatively new to Swift and integrating with Objective C. I'm curious if there's a problem with the respondsToSelector since the Objective C code is referencing Swift code.

The messages are definitely getting passed; there's another lower level function in the PubNub library that's logging all the messages received.

Any help would be much appreciated.

Scott Lobdell
  • 527
  • 1
  • 4
  • 11

4 Answers4

4

Versions prior to 4.0 are deprecated and wont work exactly how they used to.

I would recommend migrating over to the newest (4.0) SDK entirely, the new iOS SDK has removed a lot of bloat and compiles much faster. To get started view this tutorial.

To summarize, instantiating a PubNub client look as follows:

let config = PNConfiguration( 
    publishKey: "Your_Pub_Key", 
    subscribeKey: "Your_Sub_Key")   
client = PubNub.clientWithConfiguration(config) 
client?.addListener(self) 
client?.subscribeToChannels(["Your_Channel"], withPresence: false)       

And the new didReceiveMessage function looks as follows:

func client(client: PubNub!, didReceiveMessage message: PNMessageResult!, withStatus status: PNErrorStatus!) { 
     //Do Something like
     //println(message) 
}
Justin
  • 243
  • 5
  • 15
3

Resolved by adding:

func client(client: PubNub!, didReceiveMessage message: PNMessageResult!) {

}
Scott Lobdell
  • 527
  • 1
  • 4
  • 11
0

The documentation on how to parse the received PNMessageResult is scant. Here's how I handled it:

func client(client: PubNub!, didReceiveMessage message: PNMessageResult!) {

  let encodedMessage = message.data.valueForKey("message") as! NSDictionary
  let messageType    = encodedMessage["meta"]! as! String
  let messageString  = encodedMessage["data"]!["msg"]! as! String

  print("PubNub: [\(messageType)] \(messageString)") 
}
Andy
  • 1,801
  • 3
  • 22
  • 33
0

add _ client works for me!

func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
    print("Pubnub Message: \(message)")
}
webmastx
  • 683
  • 1
  • 8
  • 30