-->

iOS XMPP PubSub not receiving events while publish

2020-04-11 18:43发布

问题:

I am using XMPPClient with ejjaberd for my chat application(like Whatsapp). I want to implement XMPPPubsub to notify all users when any one of user changed his/her profile picture.

My framework : https://github.com/robbiehanson/XMPPFramework

Here is my code

Initialize XMPPPubsub

XMPPJID *serviceJID =[XMPPJID jidWithString:[NSString stringWithFormat:@"pubsub.%@",[[SharedClass sharedInstance] hostName]]];
     _xmppPubSub = [[XMPPPubSub alloc]initWithServiceJID:serviceJID     dispatchQueue:dispatch_get_main_queue()];
    [_xmppPubSub addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [_xmppPubSub activate:xmppStream];

To create node :

NSString *nodeName =[[NSUserDefaults standardUserDefaults] valueForKey:@"kmobileNo"]; // logged in user or current user
[[[XmppClient sharedInstance] xmppPubSub] createNode:nodeName withOptions:@{@"pubsub#title":nodeName,@"pubsub#deliver_notifications":@"1",@"pubsub#subscribe":@"1",@"pubsub#presence_based_delivery":@"1",@"pubsub#publish_model":@"open",@"pubsub#access_model":@"open",@"pubsub#persist_items":@"1",@"pubsub#notify_sub":@"1",@"pubsub#deliver_payloads":@"1"}];

To subscribe users

for (Contact *obj in arrayUsers) {
        NSLog(@"elsa user %@",obj.phoneNumber);
        [[XmppClient sharedInstance].xmppPubSub subscribeToNode:obj.phoneNumber withJID:[XmppClient sharedInstance].xmppStream.myJID options: @{ @"pubsub#deliver"      : @(YES),
                                                                                                                                                 @"pubsub#digest"       : @(YES),
                                                                                                                                                 @"pubsub#include_body" : @(YES),
                                                                                                                                             @"pubsub#show-values"  : @[ @"chat", @"online", @"away" ] }];
  }

To publish events :

    NSString *nodeName =[[NSUserDefaults standardUserDefaults] valueForKey:@"kmobileNo"];


   NSXMLElement *body = [NSXMLElement elementWithName:@"body"];
            [body setStringValue:@"String to post"];

   NSXMLElement *messageBody = [NSXMLElement elementWithName:@"message"];
   [messageBody setXmlns:@"jabber:client"];
   [messageBody addChild:body];
   [[[XmppClient sharedInstance] xmppPubSub] publishToNode:nodeName entry:messageBody withItemID:nil options:@{@"pubsub#access_model":@"open"}];

My problems are

  1. I am not receiving events on below delegate , when user publish node to my subscribed users

    -(void)xmppPubSub:(XMPPPubSub *)sender didReceiveMessage:(XMPPMessage *)message { NSLog(@"Message %@",message); }

But I am getting those all events correctly on above delegate when I create my node. I am creating my node after initialise pubsub. So I am getting all my events when I launch my app Because I am initializing pubsub on Appdelegate.

also I am receiving same events continously whenever I launch app. For example , If I have event(profile pic changed) to be received then I will get it on "didReceiveMessage" on launch of application. I will get the same on next every launches.

I want to get events on "didReceiveMessage" delegate when someone publish to my subscribed users(when user change profile picture) , not when launch app(when create node).

  1. How to get all users who are all subscribed by me. ?

3.How to know node is already created with my number ?

4.I want to know why I am getting events when creating node instead of when user publish to node ?

5.Why I am receiving same events again and again whenever I create node ? Please help me . Thanks in advance.