Basic IQ Handler¶
XMPP stands for Extensible Messaging and Presence Protocol. One way the protocol can be extended is by defining new types of queries, or IQs, that XMPP entities should be able to handle. It's usual that a XEP defining some XMPP extension contains some new type of IQ. IQs can also be used to implement custom features - required in a particular problem domain - but not defined by any official XEP.
This tutorial will show you how to add and test a simple module with an IQ
handler to MongooseIM.
gen_iq_handler
module provides functionality for registering IQ
handlers for specific namespaces.
Clone & build¶
See How-to-build for details on building MongooseIM from source code.
Create a module & add a basic IQ handler¶
Go to src/
and create a basic module implementing the gen_mod
behaviour.
In start/2
register the IQ handler with a specified namespace, type
(IQ processing policy), and function which will handle the incoming IQ stanza.
In stop/1
remove the registered handler.
Implement the function for handler:
-
If the incoming IQ stanza is of type
get
orset
it will be returned with the type set toresult
. -
If the server doesn't recognise the hostname, the returning stanza will be of type
error
.
See Server Rules for Processing XML Stanzas for more detailed information on the topic.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Test your handler¶
Go to big_tests/tests
and create a test suite for your handler.
Implement the test case for success and failure.
We will register two users, which are predefined in $REPO/big_tests/test.config
:
1 2 3 4 5 6 7 8 9 |
|
Our IQ handler will be enabled only for one domain, localhost
.
After sending an IQ stanza to alice
we should get a result, but as our IQ
handler is not enabled for localhost.bis
domain, we should get an error.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
|
Run it¶
Compile & generate releases for testing purposes according to
How-to-build.
Go to $REPO/_build/mim1/rel/mongooseim
and start one MongooseIM node.
1 |
|
$REPO
and use the test runner.
Run single suite with the already started mim1
node.
1 2 |
|