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 object in Strophe.js. It manages the full lifecycle of an XMPP session — from initial TCP or WebSocket handshake through SASL authentication, resource binding, and eventual disconnection. Every operation in your XMPP client flows through a Connection instance: sending stanzas, registering handlers, and reacting to server-driven events all happen via this class.
Creating a Connection
Instantiate a connection by passing a service URL and an optional configuration object:service parameter determines both the endpoint and the transport protocol. You can also pass a path-only URL when connecting to the same host as the page:
Transport Selection
Strophe.js automatically selects a transport based on the service URL scheme:| URL prefix | Transport |
|---|---|
ws:// or wss:// | WebSocket |
http://, https://, or a relative path | BOSH |
Relative path + protocol: 'ws' or 'wss' option | WebSocket |
When using a relative URL without a protocol prefix, security is never downgraded: if the page is served over HTTPS, Strophe.js will use the secure variant of whichever transport is selected (
wss:// or https://).worker option with the URL of the shared worker script. This lets multiple browser tabs share a single underlying WebSocket connection.
Key Properties
Once a connection is created and in use, several properties expose the current session state:The full JID assigned to this session (e.g.
alice@example.com/resource). Empty string before connection.The authorization identity — the bare JID derived from the supplied JID at connect time.
The XMPP domain extracted from the JID (e.g.
example.com). Populated after connect() is called.true when the session is fully established and authenticated. Handlers are only dispatched while this is true.true while a graceful disconnection is in progress.When
true, Strophe.js will not send any more requests to the server. Useful for batching multiple send() calls into a single BOSH request. Call conn.pause() / conn.resume() to toggle.The maximum number of connection retries before permanently disconnecting. Defaults to
5.Connection Status Constants
Every status update delivered to your connect callback uses a numeric constant fromStrophe.Status. The full set of values is:
| Constant | Value | Meaning |
|---|---|---|
Strophe.Status.ERROR | 0 | An unrecoverable error occurred. |
Strophe.Status.CONNECTING | 1 | The connection is being established. |
Strophe.Status.CONNFAIL | 2 | The connection attempt failed. |
Strophe.Status.AUTHENTICATING | 3 | SASL authentication is in progress. |
Strophe.Status.AUTHFAIL | 4 | Authentication failed (bad credentials or no matching mechanism). |
Strophe.Status.CONNECTED | 5 | The session is fully established and ready to use. |
Strophe.Status.DISCONNECTED | 6 | The session has ended. |
Strophe.Status.DISCONNECTING | 7 | A graceful disconnection is in progress. |
Strophe.Status.ATTACHED | 8 | Successfully attached to a pre-existing BOSH session. |
Strophe.Status.REDIRECT | 9 | The server sent a redirect. |
Strophe.Status.CONNTIMEOUT | 10 | The connection timed out. |
Strophe.Status.BINDREQUIRED | 11 | Resource binding is required; only fired when explicitResourceBinding is true. |
Strophe.Status.ATTACHFAIL | 12 | Attaching to a BOSH session failed. |
Strophe.Status.RECONNECTING | 13 | The client is attempting to reconnect after an interruption. |
The normal happy path flows through:
CONNECTING → AUTHENTICATING → CONNECTED. On failure it branches to CONNFAIL, AUTHFAIL, or ERROR. When you call disconnect(), the sequence is DISCONNECTING → DISCONNECTED.Connecting and the Status Callback
Callconn.connect() to start a session. The callback you provide will be invoked each time the connection status changes. Return nothing from the callback — it is purely for side effects.
Disconnecting
To end a session gracefully, calldisconnect(). You can pass an optional reason string that will be included in the XMPP stream close:
</stream:stream> close and wait up to disconnection_timeout milliseconds (default 3000) for the server to acknowledge before forcefully tearing down the transport. The callback will receive DISCONNECTING then DISCONNECTED.
Sending Stanzas
Usesend() to push a built stanza onto the outgoing queue:
conn.flush().
For IQ stanzas, the higher-level sendIQ() helper automatically manages the response handler and supports an optional timeout:
sendPresence().
Debugging with Raw and XML Hooks
Strophe.js exposes four overrideable hooks for inspecting traffic at different levels of abstraction. By default they are no-ops.Protocol Error Handlers
You can register a callback for specific HTTP or WebSocket error codes that may occur at the transport layer:'HTTP' (for BOSH) and 'websocket'.
BOSH Session Persistence
When using BOSH withkeepalive: true, Strophe.js caches the session tokens (RID and SID) in sessionStorage. On subsequent page loads you can restore the session instead of re-authenticating:
conn.attach().