Establish callback in Swift for PubNub 4.0 to receive messages

536 Views Asked by At

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.

4

There are 4 best solutions below

5
Scott Lobdell On

Resolved by adding:

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

}
0
Justin On

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) 
}
0
Andy On

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)") 
}
0
webmastx On

add _ client works for me!

func client(_ client: PubNub, didReceiveMessage message: PNMessageResult) {
    print("Pubnub Message: \(message)")
}