In XMPP, a stanza is a discrete XML element exchanged between a client and a server. The three core stanza types defined by the protocol areDocumentation 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.
<message> (for chat and notifications), <iq> (Info/Query — for request/response interactions), and <presence> (for availability broadcasts). Custom extension elements can be nested inside any of these. Strophe.js provides two complementary APIs for constructing stanzas: the fluent Builder class and the stx tagged template literal introduced in 3.1.0.
The Builder Class
Builder provides a jQuery-inspired fluent interface for constructing XML trees programmatically. Each method (except tree() and toString()) returns the Builder instance so calls can be chained. The internal cursor tracks which element new children will be appended to — c() moves the cursor down, up() moves it back to the parent.
Shorthand Factory Functions
Four convenience functions create pre-configuredBuilder instances. These are available as named exports and as globals ($build, $msg, $iq, $pres) when using the UMD bundle:
| Function | Root element | Auto-namespace |
|---|---|---|
$build(name, attrs?) | Any element name | None |
$msg(attrs?) | <message> | jabber:client |
$iq(attrs?) | <iq> | jabber:client |
$pres(attrs?) | <presence> | jabber:client |
$msg, $iq, and $pres automatically inject xmlns="jabber:client" when no xmlns attribute is supplied. This ensures stanzas are valid according to RFC 6120 without requiring you to remember the namespace on every call.Builder Methods
Append a child element and move the cursor to it. If
text is provided, a text node is added and the cursor stays on the parent (convenient for single-line leaf nodes).Append an existing DOM
Element (or another Builder) as a child and move the cursor to it. The element is imported/copied into the current document.Append a text node to the current element. Does not move the cursor.
Replace the current element’s content with an XHTML-filtered version of the provided HTML string (for XEP-0071 XHTML-IM payloads).
Move the cursor back to the parent element.
Move the cursor directly back to the root element, regardless of nesting depth.
Add or modify attributes on the current element. Pass
null as a value to remove an attribute. Does not move the cursor.Return the root DOM
Element. Required when passing the stanza to conn.send() directly (though send() also accepts Builder objects).Serialize the entire XML tree to a string.
Building Stanzas with the Builder API
Traversal and Multiple Children
When you need to add two siblings, call.up() after the first child to return the cursor to the parent before adding the second:
The stx Tagged Template Literal
stx is an ES2015 tagged template function that lets you write stanzas as inline XML strings. Interpolated values are automatically XML-escaped, preventing injection attacks when you embed user-supplied data into stanza text content.
Interpolating Other Builders and Stanzas
stx accepts other Builder and Stanza instances as interpolated values. When a Builder is interpolated it is serialized in-place, without escaping:
Stanza.unsafeXML() — Bypassing Escaping
When you have a pre-serialized XML fragment that you want to embed without escaping, wrap it in Stanza.unsafeXML():
unsafeXML with strings you fully control. Never pass user input through unsafeXML.
Equivalent Examples: Builder vs stx
Parsing XML Strings into Elements
ThetoStanza() utility (an alias for Stanza.toElement()) parses an XML string into a DOM Element. It validates that message, iq, and presence elements carry a valid namespace (jabber:client or jabber:server):
Builder.fromString() method to get a Builder wrapping a parsed element, enabling further chaining: