7

I would like to create a simple XMPP client in java that shares his location (XEP-0080) with other clients. I already know I can use the smack library for XMPP and that it supports PEP, which is needed for XEP-0080. Does anyone have an example how to implement this or any pointers, i don't find anything using google.

thanks in advance.

Flow
  • 23,572
  • 15
  • 99
  • 156
Kristof
  • 557
  • 6
  • 14

3 Answers3

8

Kristof's right, the doc's are sparse - but they are getting better. There is a good, albeit hard to find, set of docs on extensions though. The PubSub one is at http://www.igniterealtime.org/fisheye/browse/~raw,r=11613/svn-org/smack/trunk/documentation/extensions/pubsub.html.

After going the from scratch custom IQ Provider route with an extension I found it was easier to do it using the managers as much as possible. The developers that wrote the managers have abstracted away a lot of the pain points.

Example (modified-for-geoloc version of one rcollier wrote on the Smack forum):

ConfigureForm form = new ConfigureForm(FormType.submit);
form.setPersistentItems(false);
form.setDeliverPayloads(true);
form.setAccessModel(AccessModel.open);

PubSubManager manager 
      = new PubSubManager(connection, "pubsub.communitivity.com");
Node myNode = manager.createNode("http://jabber.org/protocol/geoloc", form);

StringBuilder body = new StringBuilder(); //ws for readability
body.append("<geoloc xmlns='http://jabber.org/protocol/geoloc' xml:lang='en'>");
body.append("   <country>Italy</country>");
body.append("   <lat>45.44</lat>");
body.append("   <locality>Venice</locality>");
body.append("   <lon>12.33</lon>");
body.append("   <accuracy>20</accuracy>");
body.append("</geoloc>");

SimplePayload payload = new SimplePayload(
                              "geoloc",
                              "http://jabber.org/protocol/geoloc", 
                              body.toString());
String itemId = "zz234";
Item<SimplePayload> item = new Item<SimplePayload>(itemId, payload);

// Required to recieve the events being published
myNode.addItemEventListener(myEventHandler);

// Publish item
myNode.publish(item);

Or at least that's the hard way :). Just remembered there's a PEPManager now...

PEPProvider pepProvider = new PEPProvider();
pepProvider.registerPEPParserExtension(
    "http://jabber.org/protocol/tune", new TuneProvider());
ProviderManager.getInstance().addExtensionProvider(
   "event", 
   "http://jabber.org/protocol/pubsub#event", pepProvider);
Tune tune = new Tune("jeff", "1", "CD", "My Title", "My Track");
pepManager.publish(tune);

You'd need to write the GeoLocProvider and GeoLoc classes.

Bill Barnhill
  • 983
  • 11
  • 17
  • where can i find this `TuneProvider` and `Tune` class in smack or asmack ? – Hunt Jul 31 '12 at 05:25
  • 1
    The example is from the smack documentation. Not sure what implementation of XEP-0118 they are referring too, as Smack still has an open bug to add XEP-0118 support, last update in May 2011. I had cobbled together my own version for an experiment, but don't have the code anymore. I'd throw it together again but don't have the time. There is now a good example of creating your own extension of PEPItem and publishing an item here: http://oneminutedistraction.wordpress.com/2010/08/26/ . That just leaves the parsing part (in the TuneProvider, a PEPProvider). – Bill Barnhill Aug 04 '12 at 16:04
  • I'm using smack 4.2.0 and I can't find PEPProvider ? – umerk44 Sep 13 '17 at 10:27
1

I covered a pure PEP based approach as an alternative method in detail for Android here: https://stackoverflow.com/a/26719158/406920.

This will be very close to what you'd need to do with regular Smack.

Community
  • 1
  • 1
dbotha
  • 1,501
  • 4
  • 20
  • 38
0

Take a look at the existing code for implementations of other extensions. This will be your best example of how to develop with the current library. Unfortunately, there is no developers guide that I know of, so I just poked around to understand some of the basics myself until I felt comfortable with the environment. Hint: Use the providers extension facility to add custom providers for the extension specific stanzas.

You can ask questions on the developer forum for Smack, and contribute your code back to the project from here as well. If you produce an implementation of this extension, then you could potentially get commit privileges yourself if you want it.

Robin
  • 24,062
  • 5
  • 49
  • 58
  • First of all, thank you for that quick answer. Do you also know where I can find such implementations ? – Kristof Mar 05 '10 at 16:39
  • They're all part of the existing Smack codebase here (for svn access) svn co http://svn.igniterealtime.org/svn/repos/smack/trunk smack or you can browse online here http://www.igniterealtime.org/fisheye/viewrep/svn-org/smack – Robin Mar 05 '10 at 18:13