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.
Builder class is Strophe.js’s primary tool for constructing XMPP stanzas programmatically. It provides a fluent, chainable interface — similar to jQuery — for building XML trees without manually juggling DOM APIs. Every method that mutates the tree returns the Builder instance itself (or a child-focused instance), so you can compose complex stanzas in a single expression. Four shorthand factory functions ($build, $msg, $iq, $pres) cover the most common cases and save you from calling new Builder(...) directly.
Shorthand Helper Functions
These module-level functions are the recommended way to start building stanzas. They are exported directly fromstrophe.js and also registered on globalThis for environments that prefer global access.
$msg, $iq, and $pres automatically inject xmlns='jabber:client' on the root element if no xmlns attribute is provided. This matches the XMPP client-stream namespace and is required for well-formed client stanzas.$build
Builder rooted at an element with the given tag name. Use this for arbitrary element names that don’t match the three core XMPP stanza types.
The tag name of the root element (e.g.
"iq", "message", "stream:features").Optional attributes for the root element, expressed as a plain object where keys are attribute names and values are
string or number.$msg
Builder rooted at a <message/> element. The xmlns='jabber:client' attribute is added automatically when not supplied.
Optional attributes for the
<message/> element (e.g. to, from, type, id).$iq
Builder rooted at an <iq/> element. The xmlns='jabber:client' attribute is added automatically when not supplied.
Optional attributes for the
<iq/> element (e.g. to, type, id).$pres
Builder rooted at a <presence/> element. The xmlns='jabber:client' attribute is added automatically when not supplied.
Optional attributes for the
<presence/> element (e.g. from, type).Builder Class
TheBuilder class implements the underlying chainable XML-construction engine. You can instantiate it directly, but in practice the shorthand helpers above are more ergonomic for the standard stanza types.
Type: StanzaAttrs
$build. Values may be string or number; numbers are coerced to strings when applied to the DOM.
Constructor
Builder and prepares the root element. The element is created lazily — DOM construction is deferred until tree() or toString() is first called.
Tag name of the root element.
Attributes to apply to the root element. When
name is "message", "iq", or "presence" and no xmlns key is present, xmlns='jabber:client' is injected automatically.Instance Methods
tree()
Element of the stanza being built. This is the object you pass to connection.send(). Calling tree() does not reset the internal “current node” cursor — subsequent chained calls continue from wherever the cursor was.
The root DOM element of the XML tree.
toString()
The complete XML serialization of the stanza tree.
up()
The same
Builder instance, with the cursor now pointing at the parent element.root()
c() calls, root() is more convenient than calling up() repeatedly.
The same
Builder instance, with the cursor reset to the root element.attrs(moreattrs)
null or undefined removes the attribute entirely.
An object of attribute names to new values. Pass
null to remove an attribute.The same
Builder instance.c(name, attrs?, text?)
text is provided, a text node is added inside the child and the cursor stays on the child.
Tag name of the new child element.
Attributes for the child element as an object, or a shorthand string value.
Optional text content to add inside the child. When provided, the cursor does not descend into the child.
The same
Builder instance, with the cursor now on the newly created child (unless text was supplied).cnode(elem)
Element or another Builder as a child of the current node, then moves the cursor to that child. The element is deep-copied (or imported) before appending, so the original is not mutated.
An existing DOM element or
Builder instance to graft into the tree.The same
Builder instance, with the cursor on the appended child.t(text)
The text content to append.
The same
Builder instance.h(html)
An HTML string whose safe subset will be inserted as child nodes of the current element.
The same
Builder instance.Static Methods
Builder.fromString(str)
Builder, allowing you to use the fluent API on top of a stanza you received or constructed externally.
A well-formed XML string to parse. A parse error will be thrown if the string is invalid.
A new
Builder instance wrapping the parsed element.Builder.serialize(elem)
Element or Builder to an XML string. Returns null when elem is null. This is the same serialization logic used internally by toString(). Attribute names are sorted; text content and attribute values are XML-escaped.
The element or builder to serialize.
The serialized XML string, or
null if elem was null.