Use this file to discover all available pages before exploring further.
Strophe.js exposes a collection of utility functions on the Strophe namespace that cover the most common low-level tasks when working with XMPP: parsing and constructing JIDs, building and serializing XML elements, extracting text content, sanitizing HTML for XMPP-IM, and encoding or escaping strings. These functions are defined in src/utils.ts and src/log.ts and are re-exported directly on the Strophe global object.
A Jabber ID (JID) has the form node@domain/resource. These functions parse each component out of a full or bare JID string. All functions return null when the requested component is not present in the JID.
Strophe.getBareJidFromJid(jid)
Returns the bare JID (node@domain) by stripping the resource portion. If jid is falsy, returns null.
Returns the resource portion of a full JID — everything after the first /. Returns null for bare JIDs without a resource. Handles resources that themselves contain / characters.
These functions create, copy, traverse, and serialize XML DOM elements. Strophe.js uses a shared internal Document for element creation to ensure cross-browser compatibility.
Strophe.xmlGenerator()
Returns the shared XML Document instance used internally by Strophe.js to create new elements. The document is lazily initialized on first call using document.implementation.createDocument('jabber:client', 'strophe', null).
You rarely need to call this directly. Use Strophe.xmlElement() to create elements — it uses xmlGenerator() internally.
Strophe.xmlElement(name, attrs?, text?)
Creates and returns a new XML DOM element. Attributes can be supplied as a key/value object, a two-dimensional array of [name, value] pairs, or omitted entirely. An optional text child can be added directly.
Optional attributes as an object { key: value }, an array [['key', 'value'], ...], or a shorthand text string/number (treated as text content when no text argument is provided).
Parses a string as XML using DOMParser with MIME type 'text/xml' and returns the resulting XMLDocument. Useful for parsing raw XML strings received from the server.
const doc = Strophe.xmlHtmlNode('<message to="user@example.com"><body>Hi</body></message>');const body = doc.getElementsByTagName('body')[0];
Strophe.toElement(string, throwErrorIfInvalidNS?)
Parses an XML string and returns its first element child. Throws a Parser Error if the XML is malformed. For message, iq, and presence elements, validates that the namespace is either 'jabber:client' or 'jabber:server'.
Strophe.toElement( string: string, throwErrorIfInvalidNS?: boolean): Element
Performs a deep copy of an XML DOM element, duplicating all attributes and child nodes recursively. Returns a new Element for element nodes, a new Text for text nodes, or undefined for other node types.
Strophe.copyElement(node: Node): Element | Text | undefined
The DOM node to copy. Only ElementType.NORMAL (element) and ElementType.TEXT nodes are copied; other types return undefined.
const original = Strophe.xmlElement('message', { to: 'user@example.com' });const copy = Strophe.copyElement(original);// copy is an independent Element with the same attributes and children
Strophe.createHtml(node)
Converts an HTML DOM node into a sanitized XHTML-IM-compliant XML node. Only tags, attributes, and CSS properties in the Strophe.XHTML allowlist are preserved. Disallowed elements are replaced with document fragments containing their sanitized children.
An HTML DOM node to sanitize and convert. Handles element nodes (NORMAL), text nodes (TEXT), and document fragments (FRAGMENT).
// Sanitize a DOM node before embedding in an XMPP messageconst htmlNode = document.querySelector('.message-body');const xhtmlNode = Strophe.createHtml(htmlNode);const msg = $msg({ to: 'user@example.com', type: 'chat' }) .c('html', { xmlns: Strophe.NS.XHTML_IM }) .c('body', { xmlns: Strophe.NS.XHTML }) .cnode(xhtmlNode);
See the XHTML allowed values section in the Constants reference for the full list of permitted tags, attributes, and CSS properties.
Strophe.serialize(elem)
Serializes an XML Element or Strophe Builder instance into a string. This is the canonical way to convert a stanza to a string for logging or raw transmission inspection.
Strophe.serialize(elem: Element | Builder): string
Iterates over child elements of elem, calling func for each child whose tag name matches elemName. If elemName is falsy (null or empty string), func is called for every child element node.
Strophe.isTagEqual(elem, 'message'); // true if elem.tagName === 'message'
Strophe.getText(elem)
Concatenates all text node values that are direct children of elem and returns the XML-escaped result. If elem itself is a text node with no children, returns its nodeValue. Returns null if elem is null.
const bodyElem = stanza.getElementsByTagName('body')[0];const text = Strophe.getText(bodyElem);// Returns the XML-escaped text content of the <body> element
The return value is XML-escaped (e.g. & instead of &). If you need raw text, unescape it with Strophe.xmlunescape().
These functions check whether a given tag name, attribute name, or CSS property name is permitted within the XHTML-IM profile defined by XEP-0071. They are used internally by Strophe.createHtml() but are also available for custom sanitization logic.
Strophe.validTag(tag)
Returns true if tag is in the XHTML-IM allowed tag list.
Strophe.js uses an internal logging system exposed via the Strophe namespace. All log calls are gated by the current log level set via setLogLevel(). Raw wire data (sent/received XML) is logged separately through Connection.rawInput and Connection.rawOutput callbacks.
Strophe.setLogLevel(level)
Sets the minimum log level. Any log() call with a level below this threshold is silently discarded.
One of the Strophe.LOG_LEVELS values: 0 (DEBUG) through 4 (FATAL). Throws an Error if the value is outside this range.
// Show only warnings and aboveStrophe.setLogLevel(Strophe.LOG_LEVELS.WARN);// Show everything during developmentStrophe.setLogLevel(Strophe.LOG_LEVELS.DEBUG);
Strophe.log(level, msg)
Emits a log message at the specified level. Uses console.debug, console.info, console.warn, or console.error based on the level value.
Strophe.addNamespace('PING', 'urn:ietf:params:xml:ns:xmpp-ping');Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');// Now usable as:const iq = $iq({ type: 'get' }).c('ping', { xmlns: Strophe.NS.PING });
Strophe.addConnectionPlugin(name, ptype)
Registers a connection plugin so that it is automatically initialized and attached to every new Connection instance as connection[name]. Plugins allow you to extend the Connection API without modifying the core library.