mirror of
https://github.com/waku-org/js.waku.guide.git
synced 2025-02-23 12:18:15 +00:00
Replace protons
with protobufs
in relay example (#50)
This commit is contained in:
parent
bef864ebae
commit
6fe3731f0c
@ -235,23 +235,20 @@ React.useEffect(() => {
|
|||||||
# Define Message Format
|
# Define Message Format
|
||||||
|
|
||||||
To define the Protobuf message format,
|
To define the Protobuf message format,
|
||||||
use [protons](https://www.npmjs.com/package/protons)
|
you can use [protobufjs](https://www.npmjs.com/package/protobufjs):
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
npm install protons
|
npm install protobufjs
|
||||||
```
|
```
|
||||||
|
|
||||||
Define `SimpleChatMessage` with two fields: `timestamp` and `text`.
|
Define `SimpleChatMessage` with two fields: `timestamp` and `text`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
import protons from "protons";
|
import protobuf from "protobufjs";
|
||||||
|
|
||||||
const proto = protons(`
|
const SimpleChatMessage = new protobuf.Type("SimpleChatMessage")
|
||||||
message SimpleChatMessage {
|
.add(new protobuf.Field("timestamp", 1, "uint64"))
|
||||||
uint64 timestamp = 1;
|
.add(new protobuf.Field("text", 2, "string"));
|
||||||
string text = 2;
|
|
||||||
}
|
|
||||||
`);
|
|
||||||
```
|
```
|
||||||
|
|
||||||
# Send Messages
|
# Send Messages
|
||||||
@ -267,10 +264,11 @@ function sendMessage(message, waku, timestamp) {
|
|||||||
const time = timestamp.getTime();
|
const time = timestamp.getTime();
|
||||||
|
|
||||||
// Encode to protobuf
|
// Encode to protobuf
|
||||||
const payload = proto.SimpleChatMessage.encode({
|
const protoMsg = SimpleChatMessage.create({
|
||||||
timestamp: time,
|
timestamp: time,
|
||||||
text: message,
|
text: message,
|
||||||
});
|
});
|
||||||
|
const payload = SimpleChatMessage.encode(protoMsg).finish();
|
||||||
|
|
||||||
// Wrap in a Waku Message
|
// Wrap in a Waku Message
|
||||||
return WakuMessage.fromBytes(payload, ContentTopic).then((wakuMessage) =>
|
return WakuMessage.fromBytes(payload, ContentTopic).then((wakuMessage) =>
|
||||||
@ -333,9 +331,8 @@ const processIncomingMessage = React.useCallback((wakuMessage) => {
|
|||||||
if (!wakuMessage.payload) return;
|
if (!wakuMessage.payload) return;
|
||||||
|
|
||||||
// Decode the protobuf payload
|
// Decode the protobuf payload
|
||||||
const { timestamp, text } = proto.SimpleChatMessage.decode(
|
const {text, timestamp} = SimpleChatMessage.decode(wakuMessage.payload);
|
||||||
wakuMessage.payload
|
|
||||||
);
|
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
time.setTime(timestamp);
|
time.setTime(timestamp);
|
||||||
|
|
||||||
@ -378,9 +375,7 @@ function App() {
|
|||||||
const processIncomingMessage = React.useCallback((wakuMessage) => {
|
const processIncomingMessage = React.useCallback((wakuMessage) => {
|
||||||
if (!wakuMessage.payload) return;
|
if (!wakuMessage.payload) return;
|
||||||
|
|
||||||
const { text, timestamp } = proto.SimpleChatMessage.decode(
|
const {text, timestamp} = SimpleChatMessage.decode(wakuMessage.payload);
|
||||||
wakuMessage.payload
|
|
||||||
);
|
|
||||||
|
|
||||||
const time = new Date();
|
const time = new Date();
|
||||||
time.setTime(timestamp);
|
time.setTime(timestamp);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user