Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
adapter
✓ import * as utils from '@iobroker/adapter-core';
✗ const utils = require('@iobroker/adapter-core');
Use ESM imports for adapter-core to ensure proper loading.
WebSocket
✓ import WebSocket from 'ws';
✗ const WebSocket = require('ws').WebSocket;
ws package provides a default export; named import is incorrect.
AdapterClass
✓ import { Adapter } from '@iobroker/adapter-core';
✗ const { Adapter } = require('@iobroker/adapter-core');
iobroker.js-controller exports Adapter as named export. Use ES module syntax for proper tree shaking.
Shows how to create an ioBroker adapter with WebSocket connection to mempool.space, handling real-time data for conversions, fees, blocks, stats, and mempool.
import * as utils from '@iobroker/adapter-core';
import WebSocket from 'ws';
class MempoolSpace extends utils.Adapter {
constructor(options) {
super({
...options,
name: 'mempool-space',
});
this.on('ready', this.onReady.bind(this));
}
async onReady() {
// Use user-provided URL or default
const wsUrl = this.config.wsUrl || 'wss://mempool.space/api/v1/ws';
const ws = new WebSocket(wsUrl);
ws.on('open', () => {
this.log.info('Connected to mempool.space WebSocket');
this.setState('info.connection', true, true);
});
ws.on('message', (data) => {
const parsed = JSON.parse(data.toString());
// Handle 'conversions', 'fees', 'blocks', 'stats', 'mempool' actions
switch (parsed.action) {
case 'conversions':
this.setState('conversion.usd', parsed.data.usd, true);
this.setState('conversion.eur', parsed.data.eur, true);
this.setState('conversion.moscowtimeUSD', parsed.data.moscowtimeUSD, true);
this.setState('conversion.moscowtimeEUR', parsed.data.moscowtimeEUR, true);
this.setState('conversion.timestamp', parsed.data.timestamp, true);
break;
case 'fees':
this.setState('fees.fastest', parsed.data.fastestFee, true);
this.setState('fees.halfHour', parsed.data.halfHourFee, true);
this.setState('fees.hour', parsed.data.hourFee, true);
this.setState('fees.economy', parsed.data.economyFee, true);
this.setState('fees.minimum', parsed.data.minimumFee, true);
break;
case 'blocks':
this.setState('block.height', parsed.data.height, true);
this.setState('block.hash', parsed.data.hash, true);
this.setState('block.timestamp', parsed.data.timestamp, true);
this.setState('block.miningPool', parsed.data.miningPool, true);
break;
case 'stats':
this.setState('network.averageBlockTime', parsed.data.averageBlockTime, true);
this.setState('network.difficultyChange', parsed.data.difficultyChange, true);
this.setState('network.previousDifficultyChange', parsed.data.previousDifficultyChange, true);
break;
case 'mempool':
this.setState('mempool.transactionCount', parsed.data.count, true);
break;
default:
this.log.warn(`Unknown action: ${parsed.action}`);
}
});
ws.on('close', () => {
this.setState('info.connection', false, true);
this.log.info('WebSocket disconnected');
});
ws.on('error', (err) => {
this.log.error(`WebSocket error: ${err.message}`);
});
}
}
// @ts-ignore
if (require.main !== module) {
module.exports = (options) => new MempoolSpace(options);
}
Errors
Common errors & fixes
Error: Cannot find module 'ws'
Missing ws dependency; npm install without --production should include it.
fixRun 'npm install' in the adapter directory.
TypeError: utils.Adapter is not a constructor
Incorrect import of adapter-core; trying to use default export instead of named.
fixUse 'import * as utils from '@iobroker/adapter-core';' and then 'new utils.Adapter(...)'.
WebSocket connection to 'wss://mempool.space/api/v1/ws' failed: Connection refused
mempool.space server may be down or blocking the IP.
fixCheck your internet connection and try a different mempool.space instance.
Audit
Dependencies
iobroker.js-controllerrequiredRequired ioBroker runtime
wsrequiredWebSocket client for mempool.space connection