Strophe.js keeps the core workflow simple: create aDocumentation 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, call connect() with your JID, password, and a status callback, then register stanza handlers and start sending. The five steps below walk from installation to a fully working echobot — a program that replies to every chat message it receives — to show all the moving parts in context.
Step-by-step: Building an Echobot
Install Strophe.js
Add the package to your project. In Node.js also install the peer dependencies that provide DOM and WebSocket APIs:
Create a Connection
Import the library and instantiate a
Connection. The transport is selected automatically from the service URL.- WebSocket
- BOSH
Strophe.Connection accepts an optional second argument — a ConnectionOptions object — where you can configure things like custom SASL mechanisms, cookies, keepalive, and more. Omitting it uses the defaults.Connect with a Status Callback
Call The full set of
connection.connect() with the user’s JID, their password, and a callback function. Strophe.js calls the callback multiple times as the connection moves through its lifecycle, passing a numeric status code from Strophe.Status.Strophe.Status constants:| Constant | Value | Meaning |
|---|---|---|
ERROR | 0 | An unrecoverable error occurred |
CONNECTING | 1 | TCP/WebSocket handshake in progress |
CONNFAIL | 2 | Connection attempt failed |
AUTHENTICATING | 3 | SASL exchange in progress |
AUTHFAIL | 4 | Authentication rejected by server |
CONNECTED | 5 | Session is open and authenticated |
DISCONNECTED | 6 | Connection is fully closed |
DISCONNECTING | 7 | Graceful shutdown in progress |
ATTACHED | 8 | Attached to an existing BOSH session |
REDIRECT | 9 | Server issued a redirect |
CONNTIMEOUT | 10 | Disconnect timeout elapsed |
BINDREQUIRED | 11 | Server requires explicit resource binding |
ATTACHFAIL | 12 | Session attach attempt failed |
RECONNECTING | 13 | Automatic reconnect in progress |
Add Stanza Handlers
Inside your
CONNECTED branch, register handlers with connection.addHandler(). A handler receives the matching stanza as a DOM Element and must return true to stay registered — returning false (or nothing) removes the handler after the first match.Send Stanzas and Disconnect
Strophe.js provides four stanza builder helpers that mirror XMPP’s core stanza types. Call To shut down cleanly, call
.tree() on the builder to get the underlying DOM element before passing it to send().disconnect(). Strophe.js sends an unavailable presence stanza, waits up to 3 seconds (configurable via disconnection_timeout), then closes the transport.Complete Echobot Example
The snippet below combines all five steps into a self-contained module. It imports fromstrophe.js, connects over WebSocket, and echoes every incoming chat message.