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 class is the central component of Strophe.js. It manages a BOSH or WebSocket connection to an XMPP server, handles SASL authentication, dispatches incoming stanzas to registered handlers, and exposes methods for sending stanzas and controlling connection lifecycle. All client code interacts with XMPP through a Connection instance.
Constructor
Connection object. The transport protocol is selected automatically based on the service URL: URLs beginning with ws:// or wss:// use WebSocket; URLs beginning with http://, https://, or a bare path use BOSH. Passing a worker option will use a shared WorkerWebSocket transport instead.
The BOSH endpoint URL or WebSocket URL for the XMPP server. You can also pass a
relative path (e.g.
"/http-bind/" or "/xmpp-websocket/") and the current
page’s protocol and host will be used.Optional configuration object. See the ConnectionOptions
reference for the full list of available fields.
Connection Methods
connect
callback is invoked multiple times with a Strophe.Status code and an optional error condition string. User-registered handlers become active only after authentication succeeds.
The user’s JID. May be a bare JID (
user@domain) or a full JID
(user@domain/resource). Omitting the node part triggers SASL OAUTHBEARER
or SASL ANONYMOUS authentication.The user’s plaintext password, or a pre-computed SCRAM key object with the
shape
{ name, ck, sk, iter, salt }.Invoked on every connection status change.
status is one of the
Strophe.Status constants. condition carries an RFC 3920 error condition
string on failure, or null on success. elem is the triggering XML element
when available.BOSH only. The HTTPBIND
wait attribute in seconds — how long the server
holds an open request before returning an empty response. Defaults to 60.BOSH only. The HTTPBIND
hold attribute — how many simultaneous requests the
server keeps open. Almost always 1 (the default).BOSH only. An optional
route attribute forwarded verbatim to the BOSH
connection manager.An alternative authentication identity (username) used for impersonation or
SASL EXTERNAL. When omitted, the node part of
jid is used.Milliseconds to wait for a clean disconnect before forcing
_doDisconnect. Defaults to 3000.attach
WorkerWebsocket transport, pass a function instead of a JID to hand off the worker connection. Throws a SessionError for unsupported transports.
For BOSH connections, the full JID already bound by the session (e.g.
alice@example.com/resource). For WorkerWebsocket connections, a callback
function used to hand off the worker.The BOSH session ID (
sid) returned by the session manager.The next request ID (
rid) to use. This is the value the BOSH connection
manager expects on the next request.The connect callback, same signature as for
connect().BOSH
wait value in seconds. Defaults to 60.BOSH
hold value. Defaults to 1.BOSH
window value — the allowed range of valid request IDs. Defaults to 5.restore
sessionStorage by a previous page load. Requires the keepalive: true option to have been set when the original connection was created. Must be called instead of connect() or attach() to resume. Throws a SessionError when the connection is not BOSH or sessionStorage is unavailable.
Optional JID hint used to locate the stored session tokens.
The connect callback, same signature as for
connect().BOSH
wait value in seconds.BOSH
hold value.BOSH
window value.disconnect
<unavailable> presence stanza and a BOSH terminate body (or WebSocket close frame), then waits up to disconnection_timeout milliseconds before forcing the connection closed. When called before the connection is established, it aborts all pending requests immediately.
A human-readable string describing why the disconnection occurred. Logged and
passed to the connect callback as the
condition argument.flush
send() calls when you need data to leave the browser right away rather than waiting for the next idle period.
pause
send() queue stanzas locally but nothing is transmitted to the server. Useful for batching many stanzas together before sending.
resume
pause(), allowing the connection to send all queued stanzas.
bind
explicitResourceBinding: true was set in the connection options, in which case the client must call bind() manually after receiving Strophe.Status.BINDREQUIRED.
bind() has no effect if do_bind is false — i.e. the server has not yet
advertised the urn:ietf:params:xml:ns:xmpp-bind stream feature.send
flush() is called. Both Builder instances and raw DOM Element objects are accepted.
A single stanza as a DOM
Element, a Strophe Builder object, or an array
mixing both types.sendIQ
type="result", callback is called with the response element. When the server replies with type="error", or when the optional timeout fires, errback is called (with null on timeout). Returns the id attribute that was used.
The IQ stanza to send. If no
id attribute is present, one is generated
automatically via getUniqueId("sendIQ").Called with the
<iq type="result"> response element.Called with the
<iq type="error"> element, or null when a timeout occurs.Milliseconds to wait for a response before calling
errback(null).The
id attribute of the sent IQ stanza.sendPresence
id. Useful when you need to detect a directed presence reply (e.g. leaving a MUC room) or an error response.
The presence stanza. An
id is injected if not present.Called when a non-error response presence with the same
id is received.Called with an error presence element, or
null on timeout.Milliseconds before
errback(null) is called.The
id attribute of the sent presence stanza.Handler Methods
addHandler
true to remain active, or false to be automatically unregistered after the first match.
Save the return value if you need to remove the handler manually with deleteHandler().
User-registered handlers only fire after authentication succeeds.
Handlers registered with
_addSysHandler() (internal API) fire from the start.Callback invoked with the matching DOM stanza element. Return
true to keep
the handler active; return false to remove it.XML namespace to match against the stanza element or any of its immediate
children. Pass
null to match any namespace.Stanza tag name to match (e.g.
"message", "iq", "presence"). Pass
null to match any tag name.The
type attribute value to match. Pass an array to match any of several
types. Pass null to match any type.Stanza
id attribute to match exactly. null matches any id.Stanza
from attribute to match. When options.matchBareFromJid is true,
comparison uses bare JIDs. null matches any sender.An opaque
Handler reference. Pass this to deleteHandler() to unregister.addTimedHandler
period milliseconds for as long as it returns true. Returning false removes the handler. Save the return value to remove the handler early with deleteTimedHandler().
Timed handlers become active only after authentication succeeds.
Interval in milliseconds between invocations.
Callback that takes no arguments. Return
true to reschedule; return false
to remove the handler.An opaque
TimedHandler reference for use with deleteTimedHandler().deleteHandler
handRef must be the value returned by addHandler(), not the original callback function. Deletion is deferred to the next idle loop to avoid modifying the handler list during iteration.
The
Handler reference returned by addHandler().deleteTimedHandler
handRef must be the value returned by addTimedHandler(). Deletion is deferred to the next idle loop.
The
TimedHandler reference returned by addTimedHandler().Properties
The full JID of the connected user, set immediately when
connect() is called
and updated with the server-assigned resource after resource binding completes.The authorization identity — the bare JID derived from
jid at connect time.
Used in SASL exchanges to identify who the client is acting on behalf of.The XMPP domain extracted from
jid (the part between @ and /). Set
during connect().The service URL passed to the constructor. Reflects the BOSH endpoint or
WebSocket URL in use.
true when the connection to the server is established and authenticated.
Set to false during disconnection.true while a graceful disconnection is in progress (after disconnect() is
called but before the connection is fully closed).true after pause() is called, false after resume(). When true, no
outgoing data is transmitted.true when the connection was established via restore() from a cached BOSH
session rather than a fresh connect().The full options object passed to the constructor (or an empty object if none
was supplied). You can mutate properties like
options.sync at runtime.The maximum number of reconnection attempts before the connection is abandoned
and
Strophe.Status.CONNFAIL is fired. Defaults to 5. You can increase
this on an existing connection instance before calling connect().Event Hooks
These methods exist as no-ops onConnection.prototype. Override them directly on a connection instance to intercept raw and structured traffic without registering a handler.
xmlInput
The received XML node. For BOSH connections, stanzas arrive wrapped in a
<body> element. For WebSocket, the opening/closing <stream> tags are
passed as self-closing elements.xmlOutput
The outgoing XML element.
rawInput
The raw received string (e.g. the full HTTP response body for BOSH, or the
raw WebSocket text frame payload).
rawOutput
The raw outgoing string.
Static Methods
Connection.addConnectionPlugin
Connection object. The plugin is initialized by calling ptype.init(connection) immediately after the Connection constructor runs. This is the standard extension point for Strophe plugins.
The property name under which the plugin will be accessible on the connection
instance (e.g.
"muc" → conn.muc).The plugin prototype object. Must implement an
init(connection) method.