XML-RPC Client and Server
The `xmlrpc` package provides a pure JavaScript implementation of an XML-RPC client and server for Node.js environments. Its core differentiator is the exclusive use of JavaScript libraries for XML parsing (via `sax-js`) and XML building (via `xmlbuilder`), thereby avoiding native C dependencies and their associated build requirements. This allows developers to set up XML-RPC communication without needing a C compiler or specific system libraries. The package supports both client and server roles, enabling applications to make and receive XML-RPC method calls. Key features include configurable ISO 8601 date/time formatting for both encoding and decoding, and optional support for HTTP cookies to maintain session state. The latest stable version is 1.3.2, released in June 2016. Due to its age, it exhibits an effectively abandoned release cadence, with no updates in nearly a decade.
Common errors
-
SyntaxError: Cannot use import statement outside a module
cause Attempting to use ES module `import` syntax to load the CommonJS-only `xmlrpc` package.fixChange your import statement to `const xmlrpc = require('xmlrpc');`. -
Method 'yourMethodName' does not exist
cause The XML-RPC server received a method call for which no corresponding event listener was registered using `server.on('methodName', ...)`.fixEnsure that `server.on('methodName', function(err, params, callback){...})` is defined for every method the server is expected to handle. Implement a `server.on('NotFound', ...)` handler for debugging unhandled method calls. -
Date/Time format mismatch or parsing errors in XML-RPC communication.
cause The default ISO 8601 date formatting used by `xmlrpc` for encoding does not match the specific variant expected by the receiving XML-RPC endpoint, or vice-versa.fixUse `xmlrpc.dateFormatter.setOpts(options)` to precisely configure the date encoding options (e.g., `colons`, `hyphens`, `local`, `ms`, `offset`) to match the requirements of your XML-RPC communication partners.
Warnings
- gotcha The `xmlrpc` package has not been updated since June 2016 (v1.3.2). This signifies a lack of active maintenance, posing compatibility risks with modern Node.js versions (e.g., ESM, current HTTP standards), potential unpatched security vulnerabilities in its own code or its `sax` and `xmlbuilder` dependencies, and general instability when integrated into contemporary applications.
- breaking Support for Node.js v0.8 was explicitly dropped in version 1.3.2. Applications running on very old Node.js environments may encounter issues or require a downgrade to an earlier `xmlrpc` version.
- gotcha This package is a CommonJS module and does not natively support ES module `import` syntax. Attempting to use `import xmlrpc from 'xmlrpc'` in a pure ESM context will result in a runtime error.
- gotcha The underlying XML builder library (`xmlbuilder`) saw significant version updates across `xmlrpc` releases (e.g., v2.4 to v8.2). While `xmlrpc` aims to abstract this, large jumps in internal dependency versions can sometimes introduce subtle changes in XML output or parsing behavior, particularly with edge cases.
Install
-
npm install xmlrpc -
yarn add xmlrpc -
pnpm add xmlrpc
Imports
- xmlrpc
import xmlrpc from 'xmlrpc'
const xmlrpc = require('xmlrpc') - createServer
import { createServer } from 'xmlrpc'const server = xmlrpc.createServer({ host: 'localhost', port: 9090 }) - createClient
import { createClient } from 'xmlrpc'const client = xmlrpc.createClient({ host: 'localhost', port: 9090, path: '/' })
Quickstart
const xmlrpc = require('xmlrpc');
// Creates an XML-RPC server to listen to XML-RPC method calls
const server = xmlrpc.createServer({ host: 'localhost', port: 9090 });
// Handle methods not found
server.on('NotFound', function(method, params) {
console.log('Method ' + method + ' does not exist');
});
// Handle method calls by listening for events with the method call name
server.on('anAction', function (err, params, callback) {
console.log('Method call params for \'anAction\': ' + params);
// ...perform an action...
// Send a method response with a value
callback(null, 'aResult');
});
console.log('XML-RPC server listening on port 9090');
// Waits briefly to give the XML-RPC server time to start up and start
// listening
setTimeout(function () {
// Creates an XML-RPC client. Passes the host information on where to
// make the XML-RPC calls.
const client = xmlrpc.createClient({ host: 'localhost', port: 9090, path: '/'});
// Sends a method call to the XML-RPC server
client.methodCall('anAction', ['aParam'], function (error, value) {
// Results of the method response
console.log('Method response for \'anAction\': ' + value);
});
}, 1000);