mempool/examples/nodejs/websocket.ts

30 lines
723 B
TypeScript
Raw Normal View History

/* eslint-disable @typescript-eslint/no-explicit-any */
import mempoolJS from '../../src/index';
2021-02-09 16:22:48 -03:00
const init = async () => {
const { websocket } = mempoolJS();
const ws = websocket.initServer({
2021-02-09 16:22:48 -03:00
options: ['blocks', 'stats', 'mempool-blocks', 'live-2h-chart'],
});
ws.on('message', function incoming(data) {
const res = JSON.parse(data.toString());
2021-02-09 16:22:48 -03:00
if (res.blocks) {
res.blocks.forEach((block: { height: any }) => {
2021-02-09 16:22:48 -03:00
console.log(block.height);
});
}
if (res.mempoolInfo) {
console.log(res.mempoolInfo);
}
if (res.transactions) {
console.log(res.transactions);
}
if (res.mempoolBlocks) {
console.log(res.mempoolBlocks);
}
});
};
init();