Reputation: 2443
I am looking for a working example code for implementing SleekXMPP XEP 60 plugin. What i am trying to do is authenticate over TLS and subscribe to a node called "blogupdates" Then simply wait for events and get the data contained in I have searched a few days but I can't find a good working example google or SO
note that I am not subscribing to user but a node so the publisher can be anybody.
any help?
Upvotes: 3
Views: 3938
Reputation:
If you look at this thread on the SleekXMPP mailing list (Beginner - SleekXMPP - XEP-0060) I have an example Pubsub client you can experiment with and examine. I'll be tidying up and polishing those two scripts to add to the bundled examples shortly.
(EDIT: These files are now in the examples directory in the develop branch. See examples/pubsub_client.py and examples/pubsub_events.py)
For your use case, you'd do:
xmpp['xep_0060'].subscribe('pubsub.example.com', 'blogupdates')
For more advanced usage, you can also pass:
bare=False
# Subscribe using a specific resource, and not the bare JID
subscribee='[email protected]'
# Subscribe a different JID to the node, if authorized
options=data_form
# Provide subscription options using a XEP-0004 data form
However, if you are using the develop branch, I've added some newer features you may like to make it easier to deal with publish events:
# Generic pubsub event handlers for all nodes
xmpp.add_event_handler('pubsub_publish', handler)
xmpp.add_event_handler('pubsub_retract', handler)
xmpp.add_event_handler('pubsub_purge', handler)
xmpp.add_event_handler('pubsub_delete', handler)
# Use custom-named events for certain nodes, in this case
# the User Tune node from PEP.
xmpp['xep_0060'].map_node_event('http://jabber.org/protocol/tune', 'user_tune')
xmpp.add_event_handler('user_tune_publish', handler)
# ...
# The same suffixes as the pubsub_* events.
# These events are raised in addition to the pubsub_* events.
For the event handlers, you can follow the pattern:
def pubsub_publish(self, msg):
# An event message could contain multiple items, but we break them up into
# separate events for you to make it a bit easier.
item = msg['pubsub_event']['items']['item']
# Process item depending on application, like item['tune'],
# or the generic item['payload']
You can look at the XEP-0107 and related PEP plugins to see more examples, since these features were added to implement those.
So, here is what your use case would look like:
# Note that xmpp can be either a ClientXMPP or ComponentXMPP object.
xmpp['xep_0060'].map_node_event('blogupdates', 'blogupdates')
xmpp['xep_0060'].add_event_handler('blogupdates_publish', blogupdate_publish)
xmpp['xep_0060'].subscribe('pubsub.example.com', 'blogupdates')
# If using a component, you'll need to specify a JID when subscribing using:
# ifrom="[email protected]"
def blogupdate_publish(msg):
"""Handle blog updates as they come in."""
update_xml = msg['pubsub_event']['items']['item']['payload']
# Do stuff with the ElementTree XML object, update_xml
And finally, if you find yourself spending too much time searching on Google, please drop by the Sleek chatroom: [email protected]
-- Lance
Upvotes: 9