@aggregion/blockchain-mq
v1.0.13
Published
Blockchain message queue module
Downloads
53
Keywords
Readme
BlockchainMQ
Usage xample
Using with mocks
const keySource = new MockKeySource();
const dataSource = new MockDataSource<string>();
const bmq = new BlockchainMQ({
keySource,
dataSource,
ownKey: await keySource.getMyKey('my_org_id'),
pollingInterval: 1000,
});
const topic = 56; // Some topic number
// Listening for new messages
const emitter = await bmq.poll<string>(topic);
let lastCursor;
emitter.on('message', (message) => {
console.log(message);
});
emitter.on('newCursor', (cursor) => {
lastCursor = cursor; // In production, you must save cursor and resume from this another time you need polling
});
await bmq.send<string>(
topic, // Topic number
'Hello!',
'some_receiver_org_id',
);
// Broadcast message
await bmq.send<string>(
topic,
'Hello!'
);
emitter.stop(); // Stop polling
bmq.stopAll(); // You can use this method instead of calling "stop" of each emitter
Using with EOS blockchain
To use with the real blockchain, create client instance as follows (also look at blockchainMQ.e2e-spec.ts for example):
import { AggregionBlockchain } from '@aggregion/dmp-contracts';
const client = new AggregionBlockchain(nodeUrl, [
pair1.privateKey,
pair2.privateKey,
]);
const blockchainMQ = new BlockchainMQ({
keySource: new EosKeySource({
contractName: 'dmpusers',
client: client,
}),
dataSource: new EosDataSource({
contractName: 'dmpusers',
client: client,
clientAccount: 'your_orgId', // Id of your organization in orgsv2 table
}),
ownKey: {
orgId: 'your_orgId',// Id of your organization in orgsv2 table
data: 'your organization private key', // Not blockchain key! It is private part of public key saved in orgsv2 table
format: KeyFormat.PKCS1_PEM,
algo: KeyAlgo.RSA_4096,
},
pollingInterval: 500,
});