Can use gossipsub to publish messages

This commit is contained in:
Franck Royer 2021-03-10 14:24:23 +11:00
parent be47223bae
commit 5e64708111
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
6 changed files with 74 additions and 101 deletions

View File

@ -1,7 +0,0 @@
import test from 'ava';
import { asyncABC } from './async';
test('getABC', async (t) => {
t.deepEqual(await asyncABC(), ['a', 'b', 'c']);
});

View File

@ -1,32 +0,0 @@
/**
* A sample async function (to demo Typescript's es7 async/await down-leveling).
*
* ### Example (es imports)
* ```js
* import { asyncABC } from 'typescript-starter'
* console.log(await asyncABC())
* // => ['a','b','c']
* ```
*
* ### Example (commonjs)
* ```js
* var double = require('typescript-starter').asyncABC;
* asyncABC().then(console.log);
* // => ['a','b','c']
* ```
*
* @returns a Promise which should contain `['a','b','c']`
*/
export const asyncABC = async () => {
const somethingSlow = (index: 0 | 1 | 2) => {
const storage = 'abc'.charAt(index);
return new Promise<string>((resolve) =>
// later...
resolve(storage)
);
};
const a = await somethingSlow(0);
const b = await somethingSlow(1);
const c = await somethingSlow(2);
return [a, b, c];
};

51
src/lib/node.spec.ts Normal file
View File

@ -0,0 +1,51 @@
import { TextDecoder, TextEncoder } from 'util';
import test from 'ava';
import { createNode } from './node';
function delay(ms: number)
{
return new Promise(resolve => setTimeout(resolve, ms));
}
test('Can publish message', async (t) => {
const topic = 'news'
const [node1, node2] = await Promise.all([
createNode(),
createNode()
])
// Add node's 2 data to the PeerStore
node1.peerStore.addressBook.set(node2.peerId, node2.multiaddrs)
await node1.dial(node2.peerId)
let node1Received = "";
node1.pubsub.on(topic, (msg) => {
node1Received = new TextDecoder().decode(msg.data);
console.log(`node1 received: ${node1Received}`)
})
await node1.pubsub.subscribe(topic)
// Will not receive own published messages by default
node2.pubsub.on(topic, (msg) => {
console.log(`node2 received: ${new TextDecoder().decode(msg.data)}`)
})
await node2.pubsub.subscribe(topic)
const message = 'Bird bird bird, bird is the word!';
await delay(1000);
await node2.pubsub.publish(topic, new TextEncoder().encode(message));
await delay(1000);
t.deepEqual(node1Received, message)
});

23
src/lib/node.ts Normal file
View File

@ -0,0 +1,23 @@
import Libp2p from 'libp2p';
import Gossipsub from 'libp2p-gossipsub';
import Mplex from 'libp2p-mplex';
import { NOISE } from 'libp2p-noise';
import TCP from 'libp2p-tcp';
export const createNode = async () => {
const node = await Libp2p.create({
addresses: {
listen: ['/ip4/0.0.0.0/tcp/0']
},
modules: {
transport: [TCP],
streamMuxer: [Mplex],
connEncryption: [NOISE],
// @ts-ignore: Type needs update
pubsub: Gossipsub
}
})
await node.start()
return node
}

View File

@ -1,11 +0,0 @@
import test from 'ava';
import { double, power } from './number';
test('double', (t) => {
t.is(double(2), 4);
});
test('power', (t) => {
t.is(power(2, 4), 16);
});

View File

@ -1,51 +0,0 @@
/**
* Multiplies a value by 2. (Also a full example of TypeDoc's functionality.)
*
* ### Example (es module)
* ```js
* import { double } from 'typescript-starter'
* console.log(double(4))
* // => 8
* ```
*
* ### Example (commonjs)
* ```js
* var double = require('typescript-starter').double;
* console.log(double(4))
* // => 8
* ```
*
* @param value - Comment describing the `value` parameter.
* @returns Comment describing the return type.
* @anotherNote Some other value.
*/
export const double = (value: number) => {
return value * 2;
};
/**
* Raise the value of the first parameter to the power of the second using the
* es7 exponentiation operator (`**`).
*
* ### Example (es module)
* ```js
* import { power } from 'typescript-starter'
* console.log(power(2,3))
* // => 8
* ```
*
* ### Example (commonjs)
* ```js
* var power = require('typescript-starter').power;
* console.log(power(2,3))
* // => 8
* ```
* @param base - the base to exponentiate
* @param exponent - the power to which to raise the base
*/
export const power = (base: number, exponent: number) => {
/**
* This es7 exponentiation operator is transpiled by TypeScript
*/
return base ** exponent;
};