Not logged in. · Lost password · Register
Forum: agsXMPP RSS
Avatar
Orion #1
Member since Jul 2009 · 6 posts
Group memberships: Members
Show profile · Link to this post
Subject: PubSub - can't get subscription working, even stops *all* messages when attempted
I'm new to XMPP, but am extremely intrigued by all the possibilities it offers, particularly in the arena of publish/subscribe models of communication. 

I have ejabber2 installed and running locally on my computer.  I grabbed a XMPP supporting chat client (Miranda) and created a simple C# client using this wonderful AG Software SDK.  I can send basic chat messages between these two clients without problems, presence is visible, etc.

I implemented the C# client/bot to respond with random numbers if I chat "rand" to it.  Works great.

So I wanted to implement a pubsub example such that if the user subscribes to a specific node, they'll get random numbers every 10 seconds, published by this highly advanced bot.

So, in my bot, I do something like this:

    PubSubManager manager = new PubSubManager(m_Client.XMPP);
    Jid bot = new Jid("bot@mycomputername.mycomputerdomain.com");
    manager.CreateNode(bot, "mycomputername.mycomputerdomain.com/rand/get");

And then I kick off a System.Threading.Timer to call a method which generates the random number, every 10 seconds:

    Item item = new Item();
    item.Value = (m_Random.Next(100) + 100).ToString();

    PubSubManager manager = new PubSubManager(m_Client.XMPP);
    manager.PublishItem(new Jid("bot@mycomputername.mycomputerdomain.com"), "mycomputername.mycomputerdomain.com/rand/get", item);

To subscribe, I did the following:

    PubSubManager manager = new PubSubManager(client.XMPP);

    manager.Subscribe(
    new Jid("bot@mycomputername.mycomputerdomain.com"),
    new Jid("miranda_user@mycomputername.mycomputerdomain.com"),
    "mycomputername.mycomputerdomain.com/rand/get",
    new IqCB((sender, iq, data) => Console.WriteLine("Recieved!") ));

I do *not* get the random number messages when published by the bot.  And the even stranger thing is - if I execute that subscription code above, the C# client stops receiving messages from my Miranda client at all.  And if I look in my ejabberd web manager, I can see the messages queuing up for when the bot comes online.  If I don't execute that code (comment it out), messages still go through just fine even with that bot firing publish messages every 10 seconds on the Timer thread.

I can see my random number generator sending messages when I hook the OnXmlWrite event, and I see this:

    <iq id="agsXMPP_5" type="set" to="bot@mycomputername.mycomputerdomain.com"><pubsub xmlns="http://jabber.org/protocol/pubsub"><publishnode="mycomputername.mycomputerdomain.com/rand/get"><item>176</item></publish></pubsub></iq>


I also tried swapping the Jid's used in the Subscribe method call, in case I mixed those up.

So, my questions then are:

1) When I create a node/publish to a node, do I need to specify a fully qualified path, including the server?  Or should it just be "/rand/get"
2) Is it even possible to subscribe another user this way to a pubsub node?  Or does the requesting user have to make the request?
3) Is it possible to subscribe to your own pubsub?  In other words, can my bot publish the random number, but also subscribe such that it gets those messages? (just curious on that one - it seems like it should)
4) What am I doing wrong? :)

Edit: I moved the Subscription code to execute prior to the XMPPClient.Open(), and it doesn't have that strange problem where it stops accepting messages now.  But why would it matter when the connection is open?
This post was edited on 2009-07-16, 00:53 by Orion.
Avatar
Alex #2
Member since Feb 2003 · 4327 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hello Orion,

there are many questions in your post. But I think most of them are related to the same problem which is at the very beginning. I assume the node creation fails, because you are sending it to the wrong entity and you create the node at the wrong level.

The To property is the Jid of the pubsub service. Which is by default in ejabberd pubsub.yourserver.com.
Here is a small tutorial how to create nodes with the ejabberd pubsub service:
http://www.ejabberd.im/mod_pubsub-usage

So the 1st node you should create is /home/yourserver/username. Then you can create leaf nodes if you need them.

Here is a small snippet of code how I create my first node at my local ejabberd2 server with the xmpp domain localhost and the username of alex.

  1. private void cmdCreateNode_Click(object sender, EventArgs e)
  2. {
  3.     var pm = new PubSubManager(XmppCon);
  4.     pm.CreateNode(new Jid("pubsub.localhost"), "/home/localhost/alex", true, new IqCB(CreateNodeResult), null);   
  5. }
  6.  
  7. private void CreateNodeResult(object sender, IQ iq, object data)
  8. {
  9.     if (iq.Type == IqType.error)
  10.     {
  11.         // failure
  12.     }
  13.     else if (iq.Type == IqType.result)
  14.     {
  15.         // success
  16.     }
  17. }

I think the other errors are related to the same problem.

  • you send the packets to the wrong entity
  • the node was never created because of the wrong hierarchy

Alex
Avatar
Orion #3
Member since Jul 2009 · 6 posts
Group memberships: Members
Show profile · Link to this post
Great, thanks for the reply.  I think everything is working.

A quick question on what pubsub actually does though.

Which of the following statements are correct?

1)  Following my example above, if I subscribe to the "rand" service, for providing random numbers, what I get back is a "published" event that contains items.  These items may actually contain a "message" or any other XMPP stanza, but it is up to the client to "unwrap" that message and do with it as it pleases.

or

2)  If I subscribe to the "rand" service and it publishes a "message", is it *part* of the XMPP protocol to unwrap that event in the client and treat the contents as if it were sent normally without publish/subscribe?  In other words, if my rand bot publishes and random number in the format of a "message" stanza inside the "items", should I expect a chat client subscribed to that node to display a chat message with the random number? (that's not the behavior I am seeing, but wasn't sure if that's because I shouldn't, or because I'm doing something wrong)

I know those questions aren't are more a general XMPP question, but this seems like a good place to ask an expert :)
Avatar
Alex #4
Member since Feb 2003 · 4327 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Hi Orion,

none of both, but 1 is closer than 2. The payload of pubsub can be anything. This is why there are no existing general pubsub clients. Because you are unable to process and  display payloads which are not documented. There are some standardized payloads like User Tune for PEP, but the payload can be anything as long as its valid XML.

A very good example for Pubsub is the Wordpress extension described here:
http://andy.wordpress.com/2009/07/16/real-time-wordpress-c…

Think about a blog, where many readers are subscribed to. Today normally you subscribe to an ATOM or RSS feed. Some people download this feed once a day, but sometimes you want the information about new content nearly in realtime, then you poll the feed e.g. every 10 minutes. If several thousand or million people poll a feed every X minutes and there are no news most of the time this is tons of unnecessary traffic. And this is where HTTP scales very badly and webservers oftern fail over or you need to much resources. With XMPP and pubsub the problem is gone. Because you never poll teh server for updates. When an author publishes a new blog entry then its published to a pubsub node ad automatically delivered to all subscribers which is a great improvement adn teh way how it should be done with the web 2.0.

So is very inefficient and PubSub is the way to go.

Alex
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: agsXMPP RSS