xmpp.js Documentation

repository·main·Indexed 25 days ago

https://github.com/xmppjs/xmpp.js

A modular, lightweight JavaScript library for the XMPP protocol compatible with browsers, Node.js, React Native, Bun, and Deno. It includes packages such as @xmpp/client for client-side connections and @xmpp/component for XMPP components, supporting various transports (WebSocket, TCP, TLS) and SASL authentication mechanisms. Features include fast authentication (XEP-0484), dynamic resource binding, and an EventEmitter-based API for handling connection lifecycles and stanzas.

Tokens
22.3K
Snippets
48
Records
143
Agent score
80%

What's inside xmpp.js

  1. Core features and design goals of xmpp.js

    main

    xmpp.js is designed with several key principles:

    • Universal: Runs in various JavaScript environments including Browsers, Node.js, WebWorkers, React Native, Bun, Deno, GJS, and Duktape.
    • Reliable: Handles errors and automatic reconnections by default. It can be configured to loop through a list of endpoints to handle network failures without manual intervention.
    • Modular: Features are implemented as independent modules that can be added or removed easily.
    • Small: Avoids third-party dependencies. The default @xmpp/client is optimized for the web, staying under 13 kb (gzipped).
  2. Use auto-reconnect with @xmpp/client and @xmpp/component

    main
    The reconnect package provides automatic reconnection logic for @xmpp/client and @xmpp/component. This functionality is included and enabled by default in both of those packages. It works in both Node.js and browser environments. When a reconnection occurs, the entity re-uses the same options that were originally provided to its start method.
  3. Configure SASL authentication in @xmpp/client using credentials object

    main

    To enable SASL negotiation in @xmpp/client, provide a credentials object within the client configuration. This object should contain the username and password required for authentication. This method is included and enabled by default in @xmpp/client.

    import { xmpp } from "@xmpp/client";
    
    const client = xmpp({
      credentials: {
        username: "foo",
        password: "bar",
      },
    });
  4. Use Fast Authentication in @xmpp/client

    main

    Fast authentication (based on XEP-0484) is included and enabled by default in @xmpp/client.

    By default, the authentication token is stored only in memory. This means fast authentication will only be available starting from the first reconnection after the initial login. To enable fast authentication across restarts or sessions, you must provide custom functions to store, retrieve, and remove the token from a persistent storage mechanism (like a database or secure storage).

    If fast authentication fails (e.g., the token is missing or invalid), the client will automatically fall back to regular authentication.

    import { xmpp } from "@xmpp/client";
    
    const client = xmpp({
      // ... client configuration
    });
    
    // Implement persistent storage for fast authentication tokens
    client.fast.fetchToken = async () => {
      const value = await secureStorage.get("token")
      return JSON.parse(value);
    }
    
    client.fast.saveToken = async (token) => {
      await secureStorage.set("token", JSON.stringify(token));
    }
    
    client.fast.removeToken = async () => {
      await secureStorage.del("token");
    }
  5. Use a custom authentication function for dynamic SASL negotiation

    main

    Instead of a static object, you can provide an asynchronous function to the credentials option. This function is called every time authentication occurs (including reconnections), allowing for dynamic credential retrieval or interactive user input.

    The function signature is async function(authenticate, mechanisms):

    • authenticate: A function to call to complete the authentication process. It accepts credentials (an object with username and password) and the selected mechanism string.
    • mechanisms: An array of available SASL mechanisms provided by the server.

    Common use cases include:

    • Prompting the user for credentials at runtime.
    • Fetching credentials from a secure database.
    • Debugging authentication flows.
    • Implementing specific requirements for a particular SASL mechanism.
    import { xmpp } from "@xmpp/client";
    
    const client = xmpp({ credentials: onAuthenticate });
    
    async function onAuthenticate(authenticate, mechanisms) {
      console.debug("authenticate", mechanisms);
      const credentials = {
        username: await prompt("enter username"),
        password: await prompt("enter password"),
      };
      console.debug("authenticating");
      await authenticate(credentials, mechanisms[0]);
      console.debug("authenticated");
    }
  6. Use TCP transport with @xmpp/client in Node.js

    main
    The @xmpp/tcp package provides a TCP transport implementation specifically for use with @xmpp/client. If you are running @xmpp/client in a Node.js environment, this transport is included and enabled by default, allowing you to establish XMPP connections over TCP.
  7. Enable STARTTLS in @xmpp/client

    main
    The @xmpp/client package includes and enables STARTTLS negotiation by default when running in a Node.js environment. If the XMPP server supports STARTTLS, the client will automatically upgrade the existing TCP connection to a TLS-encrypted connection during the connection process.