6.8 KiB
6.8 KiB
API Documentation
Core Classes
Server
Main server class that orchestrates all components.
const Server = require('./core/server');
const server = new Server(config);
await server.initialize();
await server.start();
Methods
async initialize()- Initialize server componentsasync start()- Start all serversasync stop()- Stop all servers gracefullygetStatus()- Get server status
ConfigManager
Singleton configuration manager.
const ConfigManager = require('./core/config-manager');
const config = ConfigManager.getInstance();
await config.load('./config.yaml');
const port = config.get('network.c2s.port', 5222);
Methods
async load(configPath)- Load configurationget(key, defaultValue)- Get configuration valueset(key, value)- Set configuration valuehas(key)- Check if key existsgetAll()- Get all configuration
SessionManager
Manages active client sessions.
const session = sessionManager.createSession({
id: 'session-id',
jid: 'user@domain/resource',
type: 'c2s'
});
Methods
createSession(options)- Create new sessiongetSession(sessionId)- Get session by IDgetSessionByJid(jid)- Get session by full JIDgetSessionsByBareJid(bareJid)- Get all sessions for bare JIDgetBestSessionForBareJid(bareJid)- Get best session (highest priority)updateSession(sessionId, updates)- Update sessionauthenticateSession(sessionId, jid, resource)- Authenticate sessioncloseSession(sessionId, reason)- Close sessiongetSessionCount()- Get total session countgetAllSessions()- Get all sessions
StanzaRouter
Routes XMPP stanzas to their destinations.
stanzaRouter.route(stanza, session);
Methods
route(stanza, session)- Route a stanzaaddFilter(filter)- Add routing filterremoveFilter(filter)- Remove routing filtersendError(stanza, session, errorType)- Send error response
Events
message- Message stanza routedpresence- Presence stanza routediq- IQ stanza routedstanza:routed- Any stanza routedstanza:recipient-unavailable- Recipient not found
HostManager
Manages virtual hosts.
const host = hostManager.addHost({
domain: 'example.com',
enabled: true,
modules: ['roster', 'disco']
});
Methods
async initialize()- Initialize host manageraddHost(hostConfig)- Add virtual hostremoveHost(domain)- Remove virtual hostgetHost(domain)- Get host by domainhasHost(domain)- Check if host existsgetHosts()- Get all host domainsgetEnabledHosts()- Get enabled host domainsisLocalJid(jid)- Check if JID is local
ModuleManager
Manages server modules.
await moduleManager.loadModuleForHost('localhost', 'disco');
Methods
async initialize()- Initialize module managerregisterModuleDefinition(name, definition)- Register moduleasync loadGlobalModule(moduleName)- Load global moduleasync loadModuleForHost(host, moduleName)- Load module for hostasync loadModulesForAllHosts()- Load modules for all hostsasync unloadModule(host, moduleName)- Unload moduleasync reloadModule(host, moduleName)- Reload modulegetModule(host, moduleName)- Get module instancegetLoadedModules(host)- Get loaded module namesisModuleLoaded(host, moduleName)- Check if module loaded
StorageManager
Manages data persistence.
const store = storageManager.getStore('localhost', 'roster');
await store.set('user@localhost', rosterData);
Methods
async initialize()- Initialize storagegetStore(host, name)- Get storage storeasync shutdown()- Shutdown storage
Store
Key-value store interface.
const store = storageManager.getStore(host, name);
await store.set('key', value);
const value = await store.get('key');
await store.delete('key');
Methods
async get(key)- Get valueasync set(key, value)- Set valueasync delete(key)- Delete valueasync has(key)- Check if key existsasync keys()- Get all keysasync values()- Get all valuesasync entries()- Get all entriesasync clear()- Clear all dataasync size()- Get sizeasync find(predicate)- Find values matching predicateasync findOne(predicate)- Find first value matching predicate
Network Servers
C2SServer
Client-to-Server connection handler.
const c2s = new C2SServer(config, {
sessionManager,
stanzaRouter,
moduleManager
});
await c2s.start();
Events
session:authenticated- Client authenticated
BOSHServer
HTTP binding (BOSH) server.
const bosh = new BOSHServer(config, {
sessionManager,
stanzaRouter
});
await bosh.start();
WebSocketServer
WebSocket XMPP server.
const ws = new WebSocketServer(config, {
sessionManager,
stanzaRouter
});
await ws.start();
S2SServer
Server-to-Server federation.
const s2s = new S2SServer(config, {
sessionManager,
stanzaRouter
});
await s2s.start();
ComponentServer
External component handler.
const component = new ComponentServer(config, {
stanzaRouter
});
await component.start();
Utilities
Logger
Winston-based logger.
const Logger = require('./utils/logger');
const logger = Logger.createLogger('mymodule');
logger.info('Information message');
logger.warn('Warning message');
logger.error('Error message', error);
logger.debug('Debug message');
Data Types
Session Object
{
id: 'session-123',
jid: 'user@domain/resource',
bareJid: 'user@domain',
resource: 'resource',
authenticated: true,
type: 'c2s',
priority: 0,
presence: <presence/>,
createdAt: 1234567890,
lastActivity: 1234567890,
features: Set(['carbons', 'csi']),
metadata: {}
}
Stanza Object (ltx)
const ltx = require('ltx');
const message = new ltx.Element('message', {
to: 'user@domain',
from: 'sender@domain',
type: 'chat'
}).c('body').t('Hello!');
Module API
See MODULE_DEVELOPMENT.md for detailed module API documentation.
Error Handling
All async operations should be wrapped in try-catch:
try {
await server.start();
} catch (error) {
logger.error('Failed to start server:', error);
process.exit(1);
}
Events
The server uses EventEmitter for event-driven architecture:
sessionManager.on('session:authenticated', (session) => {
console.log('User authenticated:', session.jid);
});
stanzaRouter.on('message', (stanza, session) => {
console.log('Message received');
});
Configuration Schema
See CONFIGURATION.md for complete configuration schema.