tags:

views:

45

answers:

1

Connecting to an XMPP server with XMPPPY is simple.

from xmpp.client import Client as XMPPClient

self.xmppClient = XMPPClient("jabber.foo.com")
if not self.xmppClient.connect(server="localhost"):
    raise IOError('Cannot connect to server.')
if not self.xmppClient.auth("node", "password", "resource"):
    raise IOError('Can not auth with server.')
    self.xmppClient.RegisterHandler("message", self.messageHandler)
self.xmppClient.sendInitPresence()

However, there are times when my client has to force a disconnect, yet still keep going doing other things. I'd like to make sure that the client disconnects properly - that the socket is not 'hanging around' and the server resources are not being wasted.

Is the intended pattern to simply set the client to None and let GC clean up the object?

self.xmppClient = None

I see "disconnected handlers" in the client, but I do not see how to invoke them. And the documentation that comes with XMPPPY is horrible.

Anyone have any clue of the "right way" to disconnect?

A: 

Generally speaking, if you want to disconnect from an XMPP server, you'll send a Presence stanze with type='unavailable', like the following:

<presence type='unavailable' />

Note that Presence does NOT have a recipient address. Here's a link to XMPP's RFC in case you'd like to know more. (Section 5.1.5)

After that, you can gracefully disconnect from the server, since the presence you sent basically tells the server, "I'm out.".

I took a look at the XMPPPY documentation (Yes, I agree that it has room for improvement), and it seems xmpp.Client.Client contains a function call sendPresence(...). You might be able to send an unavailable presence using the function?

Here's the API Documentation: http://xmpppy.sourceforge.net/apidocs/xmpp.client.Client-class.html#sendPresence

DashK
You can definitely send an unavailable presence, and I am doing that. But that does not actually disconnect your client (and socket), as you've implied. You do need to gracefully disconnect from the server, which is my real question. I would have expected a corollary to xmpp.Client.Client.connect() (like disconnect()?), but there is not one.
David Gregory
@David - Sorry that I misunderstood your question. Unfortunately, I'm unfamiliar with XMPPY client. I did Google around a bit, and this is the closest thing I found for a disconnect method: http://xmpppy.sourceforge.net/examples/xsend.py In the script, the author purposely commented out a client.disconnect() call. Furthermore, if you look at the basic tutorial for the library (http://xmpppy.sourceforge.net/basic.html), the author states that it just leaves disconnection to Python and TCP/IP layer... Is it an "as-design" that the author of the library does NOT provide a disconnect method?
DashK