Processing Incoming Stanzas


Smack provides a flexible framework for processing incoming stanzas using two constructs:

The org.jivesoftware.smack.filter.StanzaFilter interface determines which specific stanzas will be delivered to a StanzaCollector or StanzaListener. Many pre-defined filters can be found in the org.jivesoftware.smack.filter package.

The following code snippet demonstrates registering both a stanza collector and a stanza listener:

// Create a stanza filter to listen for new messages from a particular
// user. We use an AndFilter to combine two other filters._
StanzaFilter filter = new AndFilter(StanzaTypeFilter.MESSAGE, 
// Assume we've created an XMPPConnection named "connection".

// First, register a stanza collector using the filter we created.
StanzaCollector myCollector = connection.createStanzaCollector(filter);
// Normally, you'd do something with the collector, like wait for new packets.

// Next, create a stanza listener. We use an anonymous inner class for brevity.
StanzaListener myListener = new StanzaListener() {
		public void processStanza(Stanza stanza) {
			// Do something with the incoming stanza here._
// Register the listener._
connection.addAsyncStanzaListener(myListener, filter);
// or for a synchronous stanza listener use
connection.addSyncStanzaListener(myListener, filter);

Standard Stanza Filters

A rich set of stanza filters are included with Smack, or you can create your own filters by coding to the StanzaFilter interface. The default set of filters includes:

Copyright (C) Jive Software 2002-2008