Strophe.js uses a handler-based model for reacting to incoming data. Rather than polling or using a single catch-all event listener, you register fine-grained handlers that fire only when stanzas matching specific criteria arrive. Two distinct handler types exist: stanza handlers that are triggered by incoming XML elements, and timed handlers that fire at regular intervals regardless of incoming traffic. Both types are managed through theDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/strophe/strophejs/llms.txt
Use this file to discover all available pages before exploring further.
Connection instance.
Stanza Handlers
A stanza handler is a callback registered withconn.addHandler(). Every incoming stanza is tested against all registered handlers. Any handler whose filter criteria match the stanza will have its callback invoked.
connection.addHandler()
Handler reference used later to unregister the handler via deleteHandler().
Parameters
The callback function. It receives the matched XML
Element as its only argument. Return true to keep the handler registered (persistent). Return false to automatically remove it after this single invocation (one-shot).The XML namespace to match against. Strophe.js checks both the element’s own
xmlns attribute and the xmlns of any direct child elements. Pass null to match stanzas regardless of namespace.The element tag name to match (e.g.
'message', 'iq', 'presence'). Pass null to match any element name.The
type attribute value to match (e.g. 'chat', 'get', 'set', 'result', 'error'). An array of strings can be passed to match any of several types. Pass null to match regardless of type.The
id attribute value to match. Used to correlate a specific response to a request. Pass null to match any id.The
from attribute value to match. By default this is a full JID comparison. Pass null to match any sender.An optional object controlling matching behaviour:
matchBareFromJid(boolean, defaultfalse): Whentrue, thefromfilter is compared using only the bare JID (node@domain), ignoring the resource. Strophe.js strips the resource from both the registeredfromvalue and the incoming stanza’sfromattribute before comparing.ignoreNamespaceFragment(boolean, defaultfalse): Whentrue, URL fragments (#suffix) are stripped from namespace URIs before comparison. Useful for protocols like XHTML-IM where namespace variants share a common base.
connection.deleteHandler()
handRef must be the value returned by addHandler():
Handler Return Value Semantics
The boolean return value from your callback controls the handler’s lifetime:| Return value | Effect |
|---|---|
true | Handler remains registered and will fire again on future matching stanzas. |
false | Handler is automatically removed after this invocation (one-shot). |
Stanza handlers only run while
connection.authenticated is true. Handlers registered before authentication is complete are queued and processed once the session is established. System-internal handlers (used by Strophe.js itself) are exempt from this restriction.Timed Handlers
A timed handler fires periodically at a fixed interval, independent of any incoming stanzas. This is useful for implementing keep-alive pings, periodic presence updates, or polling-style logic.connection.addTimedHandler()
The interval in milliseconds between invocations.
The callback function. Takes no arguments. Return
true to reschedule the handler for another period milliseconds. Return false to cancel it.TimedHandler reference for use with deleteTimedHandler().