Chat app example

This commit is contained in:
Franck Royer 2021-03-24 17:08:15 +11:00
parent e167f4fba4
commit f2c1c92353
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
4 changed files with 94 additions and 1 deletions

38
chat/index.ts Normal file
View File

@ -0,0 +1,38 @@
import Waku from '../build/main/lib/waku';
import { TOPIC } from '../build/main/lib/waku_relay';
import { Message } from '../build/main/lib/waku_message';
import readline from 'readline';
;(async function() {
const waku = await Waku.create();
console.log('Waku started');
await waku.dial('/ip4/134.209.139.210/tcp/30303/p2p/16Uiu2HAmPLe7Mzm8TsYUubgCAW1aJoeFScxrLj8ppHFivPo97bUZ');
// await waku.dial('/ip4/134.209.113.86/tcp/9000/p2p/16Uiu2HAmVVi6Q4j7MAKVibquW8aA27UNrA4Q8Wkz9EetGViu8ZF1');
console.log('Static node has been dialed');
// TODO: Automatically subscribe
await waku.relay.subscribe();
console.log('Subscribed to waku relay');
// TODO: Bubble event to waku, infere topic, decode msg
waku.libp2p.pubsub.on(TOPIC, event => {
const msg = Message.fromBinary(event.data);
console.log(msg.utf8Payload());
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
console.log('Ready to chat!');
rl.prompt();
rl.on('line', async (line) => {
rl.prompt();
const msg = Message.fromUtf8String(line);
await waku.relay.publish(msg);
});
})();

View File

@ -13,12 +13,14 @@
"build:1-proto": "buf generate",
"build:main": "tsc -p tsconfig.json",
"build:module": "tsc -p tsconfig.module.json",
"build:chat": "tsc -p tsconfig.chat.json",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.ts\" --write",
"fix:lint": "eslint src --ext .ts --fix",
"pretest": "run-s pretest:*",
"pretest:1-init-git-submodules": "[ -f './nim-waku/build/wakunode2' ] || git submodule update --init --recursive",
"pretest:2-build-nim-waku": "cd nim-waku; [ -f './build/wakunode2' ] || make -j$(nproc --all 2>/dev/null || echo 2) wakunode2",
"start-chat": "ts-node chat/index.ts",
"test": "run-s build test:*",
"test:lint": "eslint src --ext .ts",
"test:prettier": "prettier \"src/**/*.ts\" --list-different",

View File

@ -20,7 +20,7 @@ export class WakuRelayPubsub extends Gossipsub {
*/
constructor(libp2p: Libp2p) {
super(libp2p, {
emitSelf: true,
emitSelf: false,
// Ensure that no signature is expected in the messages.
globalSignaturePolicy: SignaturePolicy.StrictNoSign,
});

53
tsconfig.chat.json Normal file
View File

@ -0,0 +1,53 @@
{
"compilerOptions": {
"incremental": true,
"target": "es2017",
"outDir": "build/chat",
"rootDir": "chat",
"moduleResolution": "node",
"module": "commonjs",
"declaration": true,
"inlineSourceMap": true,
"esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
"resolveJsonModule": true /* Include modules imported with .json extension. */,
"strict": true /* Enable all strict type-checking options. */,
/* Strict Type-Checking Options */
// "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
// "strictNullChecks": true /* Enable strict null checks. */,
// "strictFunctionTypes": true /* Enable strict checking of function types. */,
// "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
// "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
// "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,
/* Additional Checks */
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
/* Debugging Options */
"traceResolution": false /* Report module resolution log messages. */,
"listEmittedFiles": false /* Print names of generated files part of the compilation. */,
"listFiles": false /* Print names of files part of the compilation. */,
"pretty": true /* Stylize errors and messages using color and context. */,
// Due to broken types in indirect dependencies
"skipLibCheck": true,
/* Experimental Options */
// "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
// "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
"lib": ["es2017"],
"types": ["node", "mocha"],
"typeRoots": ["node_modules/@types"]
},
"include": ["chat/**/*.ts"],
"exclude": ["node_modules/**"],
"compileOnSave": false,
"ts-node": {
"files": true
}
}