Skip to main content
This guide covers how to create and manage connections to Jitsi Meet servers using the JitsiConnection class.

Overview

The JitsiConnection class provides access to the Jitsi Meet server-side video conferencing service. It handles XMPP connection establishment, authentication, and provides the foundation for creating conferences.

Creating a Connection

1

Initialize JitsiMeetJS

First, initialize the library with your configuration:
JitsiMeetJS.init({
    disableAudioLevels: false,
    enableAnalyticsLogging: false
});
2

Configure connection options

Define your connection options:
const options = {
    serviceUrl: 'wss://your-server.com/xmpp-websocket',
    hosts: {
        domain: 'your-server.com'
    },
    enableWebsocketResume: true,
    websocketKeepAlive: 60000,
    websocketKeepAliveUrl: 'https://your-server.com/about/health',
    p2pStunServers: [
        { urls: 'stun:stun.l.google.com:19302' },
        { urls: 'stun:stun1.l.google.com:19302' }
    ]
};
3

Create the connection instance

Instantiate a new connection:
const connection = new JitsiMeetJS.JitsiConnection(
    null,              // appID (optional)
    null,              // JWT token (optional) 
    options
);
4

Register event handlers

Set up event listeners before connecting:
connection.addEventListener(
    JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
    onConnectionSuccess
);

connection.addEventListener(
    JitsiMeetJS.events.connection.CONNECTION_FAILED,
    onConnectionFailed
);

connection.addEventListener(
    JitsiMeetJS.events.connection.CONNECTION_DISCONNECTED,
    onDisconnect
);
5

Connect to the server

Initiate the connection:
connection.connect({
    id: '[email protected]',      // Optional: username for authentication
    password: 'password',        // Optional: password for authentication
    name: 'conferenceRoom'       // Optional: room name for HTTP conference request
});

Connection Options

Required Options

OptionTypeDescription
serviceUrlstringWebSocket URL for XMPP connection
hosts.domainstringXMPP domain name
enableWebsocketResumebooleanEnable WebSocket session resumption
p2pStunServersarraySTUN servers for P2P connections

Optional Options

interface IConnectionOptions {
    analytics?: any;
    bridgeChannel?: {
        ignoreDomain?: string;
        preferSctp?: boolean;
    };
    disableFocus?: boolean;
    flags?: Record<string, any>;
    name?: string;
    websocketKeepAlive?: number;
    websocketKeepAliveUrl?: string;
    xmppPing?: any;
}

Authentication

Anonymous Connection

Connect without credentials:
connection.connect();

Authenticated Connection

Connect with username and password:
connection.connect({
    id: '[email protected]',
    password: 'secure-password'
});

JWT Token Authentication

Provide a JWT token during connection creation:
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
const connection = new JitsiMeetJS.JitsiConnection(
    'your-app-id',
    token,
    options
);

connection.connect();

Event Handlers

function onConnectionSuccess() {
    console.log('Successfully connected!');
    // Now you can create a conference
    const conference = connection.initJitsiConference('room-name', {});
}

Managing Connections

Disconnect

Properly close the connection:
connection.disconnect().then(() => {
    console.log('Disconnected successfully');
}).catch(error => {
    console.error('Error during disconnect:', error);
});

Refresh Authentication Token

Renew JWT tokens before expiration:
const newToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';

connection.refreshToken(newToken).then(() => {
    console.log('Token refreshed successfully');
}).catch(error => {
    console.error('Failed to refresh token:', error);
});

Get Connection Info

Retrieve connection details:
// Get participant JID
const jid = connection.getJid();
console.log('My JID:', jid);

// Get connection timing information
const times = connection.getConnectionTimes();
console.log('Connection times:', times);

Feature Management

Advertise client capabilities:
// Add a feature
connection.addFeature('urn:xmpp:jingle:apps:dtls:0', true);

// Remove a feature
connection.removeFeature('urn:xmpp:jingle:apps:dtls:0', true);

Attaching to Existing Sessions

For optimized reconnection, attach to an existing XMPP session:
const attachOptions = {
    jid: '[email protected]/resource',
    sid: 'session-id',
    rid: 'request-id'
};

connection.attach(attachOptions);

Best Practices

Implement exponential backoff for reconnection attempts:
let reconnectAttempts = 0;
const maxReconnectAttempts = 5;

function reconnect() {
    if (reconnectAttempts < maxReconnectAttempts) {
        const delay = Math.min(1000 * Math.pow(2, reconnectAttempts), 30000);
        setTimeout(() => {
            reconnectAttempts++;
            connection.connect();
        }, delay);
    }
}

connection.addEventListener(
    JitsiMeetJS.events.connection.CONNECTION_FAILED,
    reconnect
);
Use connection statistics to monitor quality:
const logs = connection.getLogs();
console.log('Connection logs:', logs);
Always remove event listeners when done:
function cleanup() {
    connection.removeEventListener(
        JitsiMeetJS.events.connection.CONNECTION_ESTABLISHED,
        onConnectionSuccess
    );
    connection.removeEventListener(
        JitsiMeetJS.events.connection.CONNECTION_FAILED,
        onConnectionFailed
    );
}

Next Steps

Managing Conferences

Learn how to create and manage conferences

Handling Media Tracks

Work with audio and video tracks

Build docs developers (and LLMs) love