mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-05 22:33:08 +00:00
feat(angular): replace protons with protobufjs
As latest protons version needs to use a prot file which is not very practical for a quick example.
This commit is contained in:
parent
b80e8fcda7
commit
6e4fbccbcc
@ -3,9 +3,9 @@
|
|||||||
**Demonstrates**:
|
**Demonstrates**:
|
||||||
|
|
||||||
- Group messaging
|
- Group messaging
|
||||||
- Angular/JavaScript
|
- Angular
|
||||||
- Waku Relay
|
- Waku Relay
|
||||||
- Protobuf using `protons`
|
- Protobuf using `protobufjs`
|
||||||
- No async/await syntax
|
- No async/await syntax
|
||||||
|
|
||||||
A barebones messaging app to illustrate the [Angular Relay guide](https://docs.wakuconnect.dev/docs/guides/10_angular_relay/).
|
A barebones messaging app to illustrate the [Angular Relay guide](https://docs.wakuconnect.dev/docs/guides/10_angular_relay/).
|
||||||
|
|||||||
@ -22,7 +22,7 @@
|
|||||||
"@angular/platform-browser-dynamic": "~13.2.0",
|
"@angular/platform-browser-dynamic": "~13.2.0",
|
||||||
"@angular/router": "~13.2.0",
|
"@angular/router": "~13.2.0",
|
||||||
"js-waku": "0.24.0-f52dd9e",
|
"js-waku": "0.24.0-f52dd9e",
|
||||||
"protons": "^2.0.3",
|
"protobufjs": "^7.1.0",
|
||||||
"rxjs": "~7.5.0",
|
"rxjs": "~7.5.0",
|
||||||
"tslib": "^2.3.0",
|
"tslib": "^2.3.0",
|
||||||
"zone.js": "~0.11.4"
|
"zone.js": "~0.11.4"
|
||||||
|
|||||||
27
relay-angular-chat/pnpm-lock.yaml
generated
27
relay-angular-chat/pnpm-lock.yaml
generated
@ -23,7 +23,7 @@ specifiers:
|
|||||||
karma-coverage: ~2.1.0
|
karma-coverage: ~2.1.0
|
||||||
karma-jasmine: ~4.0.0
|
karma-jasmine: ~4.0.0
|
||||||
karma-jasmine-html-reporter: ~1.7.0
|
karma-jasmine-html-reporter: ~1.7.0
|
||||||
protons: ^2.0.3
|
protobufjs: ^7.1.0
|
||||||
rxjs: ~7.5.0
|
rxjs: ~7.5.0
|
||||||
tslib: ^2.3.0
|
tslib: ^2.3.0
|
||||||
typescript: ~4.5.2
|
typescript: ~4.5.2
|
||||||
@ -39,7 +39,7 @@ dependencies:
|
|||||||
'@angular/platform-browser-dynamic': 13.2.7_vkbm4mesp4vzs4r6epm7k663dq
|
'@angular/platform-browser-dynamic': 13.2.7_vkbm4mesp4vzs4r6epm7k663dq
|
||||||
'@angular/router': 13.2.7_uouthnyvqoiyqb2ogpzzqrqwhi
|
'@angular/router': 13.2.7_uouthnyvqoiyqb2ogpzzqrqwhi
|
||||||
js-waku: 0.24.0-f52dd9e_adbj7bocxelisufu5fxaajl5fa
|
js-waku: 0.24.0-f52dd9e_adbj7bocxelisufu5fxaajl5fa
|
||||||
protons: 2.0.3
|
protobufjs: 7.1.0
|
||||||
rxjs: 7.5.6
|
rxjs: 7.5.6
|
||||||
tslib: 2.4.0
|
tslib: 2.4.0
|
||||||
zone.js: 0.11.7
|
zone.js: 0.11.7
|
||||||
@ -7584,10 +7584,6 @@ packages:
|
|||||||
long: 5.2.0
|
long: 5.2.0
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
/protocol-buffers-schema/3.6.0:
|
|
||||||
resolution: {integrity: sha512-TdDRD+/QNdrCGCE7v8340QyuXd4kIWIgapsE2+n/SaGiSSbomYl4TjHlvIoCWRpE7wFt02EpB35VVA2ImcBVqw==}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/protons-runtime/1.0.4:
|
/protons-runtime/1.0.4:
|
||||||
resolution: {integrity: sha512-DSKWjAgwaXhtYO5Jo/MrU8n/75I/P2IhxU0Fk/lSrXx6Gxl5DH+I6cHcbGAYFmAlOBmU4QRa0mvVme8VXlDeUg==}
|
resolution: {integrity: sha512-DSKWjAgwaXhtYO5Jo/MrU8n/75I/P2IhxU0Fk/lSrXx6Gxl5DH+I6cHcbGAYFmAlOBmU4QRa0mvVme8VXlDeUg==}
|
||||||
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
|
engines: {node: '>=16.0.0', npm: '>=7.0.0'}
|
||||||
@ -7615,15 +7611,6 @@ packages:
|
|||||||
uint8arraylist: 2.3.2
|
uint8arraylist: 2.3.2
|
||||||
dev: false
|
dev: false
|
||||||
|
|
||||||
/protons/2.0.3:
|
|
||||||
resolution: {integrity: sha512-j6JikP/H7gNybNinZhAHMN07Vjr1i4lVupg598l4I9gSTjJqOvKnwjzYX2PzvBTSVf2eZ2nWv4vG+mtW8L6tpA==}
|
|
||||||
dependencies:
|
|
||||||
protocol-buffers-schema: 3.6.0
|
|
||||||
signed-varint: 2.0.1
|
|
||||||
uint8arrays: 3.1.0
|
|
||||||
varint: 5.0.2
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/proxy-addr/2.0.7:
|
/proxy-addr/2.0.7:
|
||||||
resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==}
|
resolution: {integrity: sha512-llQsMLSUDUPT44jdrU/O37qlnifitDP+ZwrmmZcoSKyLKvtZxpyV0n2/bD/N4tBAAZ/gJEdZU7KMraoK1+XYAg==}
|
||||||
engines: {node: '>= 0.10'}
|
engines: {node: '>= 0.10'}
|
||||||
@ -8123,12 +8110,6 @@ packages:
|
|||||||
/signal-exit/3.0.7:
|
/signal-exit/3.0.7:
|
||||||
resolution: {integrity: sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==}
|
resolution: {integrity: sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==}
|
||||||
|
|
||||||
/signed-varint/2.0.1:
|
|
||||||
resolution: {integrity: sha512-abgDPg1106vuZZOvw7cFwdCABddfJRz5akcCcchzTbhyhYnsG31y4AlZEgp315T7W3nQq5P4xeOm186ZiPVFzw==}
|
|
||||||
dependencies:
|
|
||||||
varint: 5.0.2
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/slash/3.0.0:
|
/slash/3.0.0:
|
||||||
resolution: {integrity: sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==}
|
resolution: {integrity: sha512-g9Q1haeby36OSStwb4ntCGGGaKsaVSjQ68fBxoQcutl5fS1vuY18H3wSt3jFyFtrkx+Kz0V1G85A4MyAdDMi2Q==}
|
||||||
engines: {node: '>=8'}
|
engines: {node: '>=8'}
|
||||||
@ -8819,10 +8800,6 @@ packages:
|
|||||||
builtins: 1.0.3
|
builtins: 1.0.3
|
||||||
dev: true
|
dev: true
|
||||||
|
|
||||||
/varint/5.0.2:
|
|
||||||
resolution: {integrity: sha512-lKxKYG6H03yCZUpAGOPOsMcGxd1RHCu1iKvEHYDPmTyq2HueGhD73ssNBqqQWfvYs04G9iUFRvmAVLW20Jw6ow==}
|
|
||||||
dev: false
|
|
||||||
|
|
||||||
/varint/6.0.0:
|
/varint/6.0.0:
|
||||||
resolution: {integrity: sha512-cXEIW6cfr15lFv563k4GuVuW/fiwjknytD37jIOLSdSWuOI6WnO/oKwmP2FQTU2l01LP8/M5TSAJpzUaGe3uWg==}
|
resolution: {integrity: sha512-cXEIW6cfr15lFv563k4GuVuW/fiwjknytD37jIOLSdSWuOI6WnO/oKwmP2FQTU2l01LP8/M5TSAJpzUaGe3uWg==}
|
||||||
dev: false
|
dev: false
|
||||||
|
|||||||
@ -1,61 +1,63 @@
|
|||||||
import { Component, OnInit } from '@angular/core';
|
import { Component, OnInit } from "@angular/core";
|
||||||
import { WakuService } from '../waku.service';
|
import { WakuService } from "../waku.service";
|
||||||
import { Waku, WakuMessage } from 'js-waku';
|
import { Waku, WakuMessage } from "js-waku";
|
||||||
import protons from 'protons';
|
import protobuf from "protobufjs";
|
||||||
|
|
||||||
const proto = protons(`
|
const ProtoChatMessage = new protobuf.Type("ChatMessage")
|
||||||
message SimpleChatMessage {
|
.add(new protobuf.Field("timestamp", 1, "uint32"))
|
||||||
uint64 timestamp = 1;
|
.add(new protobuf.Field("text", 2, "string"));
|
||||||
string text = 2;
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
|
|
||||||
interface MessageInterface {
|
interface MessageInterface {
|
||||||
timestamp: Date,
|
timestamp: Date;
|
||||||
text: string
|
text: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-messages',
|
selector: "app-messages",
|
||||||
templateUrl: './messages.component.html',
|
templateUrl: "./messages.component.html",
|
||||||
styleUrls: ['./messages.component.css']
|
styleUrls: ["./messages.component.css"],
|
||||||
})
|
})
|
||||||
export class MessagesComponent implements OnInit {
|
export class MessagesComponent implements OnInit {
|
||||||
|
|
||||||
contentTopic: string = `/relay-angular-chat/1/chat/proto`;
|
contentTopic: string = `/relay-angular-chat/1/chat/proto`;
|
||||||
messages: MessageInterface[] = [];
|
messages: MessageInterface[] = [];
|
||||||
messageCount: number = 0;
|
messageCount: number = 0;
|
||||||
waku!: Waku;
|
waku!: Waku;
|
||||||
wakuStatus!: string;
|
wakuStatus!: string;
|
||||||
|
|
||||||
constructor(private wakuService: WakuService) { }
|
constructor(private wakuService: WakuService) {}
|
||||||
|
|
||||||
ngOnInit(): void {
|
ngOnInit(): void {
|
||||||
this.wakuService.wakuStatus.subscribe(wakuStatus => {
|
this.wakuService.wakuStatus.subscribe((wakuStatus) => {
|
||||||
this.wakuStatus = wakuStatus;
|
this.wakuStatus = wakuStatus;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.wakuService.waku.subscribe(waku => {
|
this.wakuService.waku.subscribe((waku) => {
|
||||||
this.waku = waku;
|
this.waku = waku;
|
||||||
this.waku.relay.addObserver(this.processIncomingMessages, [this.contentTopic]);
|
this.waku.relay.addObserver(this.processIncomingMessages, [
|
||||||
|
this.contentTopic,
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
|
|
||||||
window.onbeforeunload = () => this.ngOnDestroy();
|
window.onbeforeunload = () => this.ngOnDestroy();
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnDestroy(): void {
|
ngOnDestroy(): void {
|
||||||
this.waku.relay.deleteObserver(this.processIncomingMessages, [this.contentTopic]);
|
this.waku.relay.deleteObserver(this.processIncomingMessages, [
|
||||||
|
this.contentTopic,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMessage(): void {
|
sendMessage(): void {
|
||||||
const time = new Date().getTime();
|
const time = new Date().getTime();
|
||||||
|
|
||||||
const payload = proto.SimpleChatMessage.encode({
|
const protoMsg = ProtoChatMessage.create({
|
||||||
timestamp: time,
|
timestamp: time,
|
||||||
text: `Here is a message #${this.messageCount}`,
|
text: `Here is a message #${this.messageCount}`,
|
||||||
});
|
});
|
||||||
|
|
||||||
WakuMessage.fromBytes(payload, this.contentTopic).then(wakuMessage => {
|
const payload = ProtoChatMessage.encode(protoMsg).finish();
|
||||||
|
|
||||||
|
WakuMessage.fromBytes(payload, this.contentTopic).then((wakuMessage) => {
|
||||||
this.waku.relay.send(wakuMessage).then(() => {
|
this.waku.relay.send(wakuMessage).then(() => {
|
||||||
console.log(`Message #${this.messageCount} sent`);
|
console.log(`Message #${this.messageCount} sent`);
|
||||||
this.messageCount += 1;
|
this.messageCount += 1;
|
||||||
@ -66,13 +68,13 @@ export class MessagesComponent implements OnInit {
|
|||||||
processIncomingMessages = (wakuMessage: WakuMessage) => {
|
processIncomingMessages = (wakuMessage: WakuMessage) => {
|
||||||
if (!wakuMessage.payload) return;
|
if (!wakuMessage.payload) return;
|
||||||
|
|
||||||
const { timestamp, text } = proto.SimpleChatMessage.decode(
|
const { text, timestamp } = ProtoChatMessage.decode(
|
||||||
wakuMessage.payload
|
wakuMessage.payload
|
||||||
);
|
) as unknown as { text: string; timestamp: bigint };
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
time.setTime(timestamp);
|
time.setTime(Number(timestamp));
|
||||||
const message = { text, timestamp: time };
|
const message = { text, timestamp: time };
|
||||||
|
|
||||||
this.messages.push(message);
|
this.messages.push(message);
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user