Minecraft RCON Client
rcon-client is a JavaScript/TypeScript library designed to connect to and interact with Minecraft servers via the RCON protocol. Its key differentiator is a built-in packet queue that limits pending requests, which is particularly useful for sending multiple commands concurrently without overwhelming the server. The library supports persistent connections, allowing an Rcon instance to connect and disconnect multiple times. It leverages async/await patterns for network operations and ships with TypeScript types, making it suitable for modern JavaScript and TypeScript projects. The current stable version is 4.2.5. However, it's explicitly stated by the maintainer that the library has not been actively maintained for a while, implying an infrequent release cadence and potential for unaddressed issues. Developers should consider this lack of maintenance when choosing this library for new projects. It uses Node.js's EventEmitter internally for connection lifecycle events.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'send')
cause Attempting to call `send()` on an `Rcon` instance that has not yet successfully connected, or after it has been explicitly `end()`ed, or if `await Rcon.connect()` was forgotten.fixEnsure `await rcon.connect()` completes successfully before calling `rcon.send()`. If using the static `Rcon.connect()`, ensure you `await` its result. Check connection status via events (`connect`, `end`, `error`) if managing connection state manually. -
Error: RCON connection failed: Authentication failed.
cause The provided password for the RCON server is incorrect.fixVerify the RCON password configured on your Minecraft server and ensure it matches the `password` field in your `Rcon.connect()` options. Double-check for typos or leading/trailing spaces. -
Error: connect ECONNREFUSED
cause The RCON client was unable to establish a TCP connection to the specified host and port. This usually means the server is not running, RCON is not enabled, or a firewall is blocking the connection.fix1. Ensure your Minecraft server is running. 2. Verify that RCON is enabled in your server's `server.properties` (`enable-rcon=true`). 3. Check the `rcon.port` and `rcon.password` in `server.properties`. 4. Confirm no firewall (OS or network) is blocking connections to the RCON port on the server host. 5. Double-check the `host` and `port` values in your `Rcon.connect()` call.
Warnings
- deprecated The `rcon-client` library is no longer actively maintained. The maintainer explicitly states this in the README, advising caution or considering alternative implementations. This means bug fixes, security updates, and new features are unlikely to be released.
- gotcha Improper handling of connection state: The `Rcon` instance supports connecting and disconnecting at any time. However, attempting to send commands on a disconnected Rcon instance will result in errors. Always ensure the client is connected before sending commands.
- gotcha Forgotten `await` for asynchronous operations. All network operations (`Rcon.connect`, `rcon.send`) return Promises and must be awaited to correctly handle their results and prevent unhandled promise rejections.
Install
-
npm install rcon-client -
yarn add rcon-client -
pnpm add rcon-client
Imports
- Rcon
const Rcon = require('rcon-client')import { Rcon } from 'rcon-client' - Rcon.connect
const rcon = Rcon.connect({ host: 'localhost', port: 25575, password: '1234' })const rcon = await Rcon.connect({ host: 'localhost', port: 25575, password: '1234' }) - RconEvents
import type { RconEvents } from 'rcon-client'
Quickstart
import { Rcon } from "rcon-client";
async function runRconCommands() {
console.log('Attempting to connect to RCON server...');
try {
const rcon = await Rcon.connect({
host: process.env.RCON_HOST ?? 'localhost',
port: parseInt(process.env.RCON_PORT ?? '25575'),
password: process.env.RCON_PASSWORD ?? '1234'
});
console.log('Connected to RCON server.');
console.log('Sending \"list\" command...');
const listResponse = await rcon.send("list");
console.log('Response from \"list\":', listResponse);
console.log('Sending \"help\" and \"whitelist list\" concurrently...');
let responses = await Promise.all([
rcon.send("help"),
rcon.send("whitelist list")
]);
console.log('Responses from concurrent commands:');
for (const response of responses) {
console.log(response);
}
rcon.end();
console.log('RCON connection ended.');
} catch (error) {
console.error('Failed to connect or send RCON command:', error);
}
}
runRconCommands();