Skip to content

Commit

Permalink
Merge pull request #24 from waku-org/weboko/change-pubsub
Browse files Browse the repository at this point in the history
feat: allow to change pubsub topic
  • Loading branch information
weboko authored Dec 6, 2023
2 parents f510cc9 + 6a0a5ed commit 48d0261
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 38 deletions.
76 changes: 53 additions & 23 deletions src/app/components/Waku.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ import { Block } from "@/components/Block";
import { Subtitle } from "@/components/Subtitle";
import { Button } from "@/components/Button";
import { MessageContent } from "@/hooks";
import { SUPPORTED_PUBSUB_TOPICS } from "@/constants";

type WakuProps = {
onSend: (nick: string, text: string) => Promise<void>;
activeContentTopic: string;
activePubsubTopic: string;
messages: MessageContent[];
onActiveContentTopicChange: (contentTopic: string) => void;
onActivePubsubTopicChange: (pubsubTopic: string) => void;
}

export const Waku: React.FunctionComponent<WakuProps> = (props) => {
Expand All @@ -19,10 +22,14 @@ export const Waku: React.FunctionComponent<WakuProps> = (props) => {
onMessageChange,
resetText,
} = useMessage();
const {
const [
contentTopic,
onContentTopicChange,
} = useContentTopic(props.activeContentTopic);
] = useTopic<HTMLInputElement>(props.activeContentTopic);
const [
pubsubTopic,
onPubsubTopicChange,
] = useTopic<HTMLSelectElement>(props.activePubsubTopic);

const onSendClick = async () => {
await props.onSend(nick, text);
Expand All @@ -38,18 +45,40 @@ export const Waku: React.FunctionComponent<WakuProps> = (props) => {
<Block className="mt-10 flex flex-col md:flex-row lg:flex-row">
<Block>
<Block>
<Subtitle>
Waku
</Subtitle>
<Subtitle>Chat</Subtitle>
</Block>

<Block className="mt-5">
<label
htmlFor="pubsubTopic"
className="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>
Pubsub topic
</label>

<select
id="pubsubTopic"
value={pubsubTopic}
onChange={onPubsubTopicChange}
className="w-96 mr-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 pr-4 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
>
{SUPPORTED_PUBSUB_TOPICS.map((v) => (
<option key={v} value={v}>{v}</option>
))}
</select>
<Button className="mt-1" onClick={() => { props.onActivePubsubTopicChange(pubsubTopic); }}>Change</Button>
</Block>

<Block className="mt-5">
<label
htmlFor="contentTopic-input"
className="block mb-2 mt-2 text-sm font-medium text-gray-900 dark:text-white"
htmlFor="contentTopic"
className="block text-sm mb-2 font-medium text-gray-900 dark:text-white"
>
Content topic
</label>
<input
type="text"
id="contentTopic-input"
id="contentTopic"
value={contentTopic}
onChange={onContentTopicChange}
className="w-96 mr-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
Expand All @@ -59,31 +88,31 @@ export const Waku: React.FunctionComponent<WakuProps> = (props) => {

<Block className="mt-4 mr-10 min-w-fit">
<label
htmlFor="nick-input"
htmlFor="nick"
className="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>
Your nickname
</label>
<input
type="text"
id="nick-input"
id="nick"
placeholder="Choose a nickname"
value={nick}
onChange={onNickChange}
className="w-96 mr-2 bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block p-2.5 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500"
/>
</Block>

<Block className="mt-4">
<Block className="mt-5">
<Block className="mb-2">
<label
htmlFor="message-input"
htmlFor="message"
className="block mb-2 text-sm font-medium text-gray-900 dark:text-white"
>
Message
</label>
<textarea
id="message-input"
id="message"
value={text}
onChange={onMessageChange}
placeholder="Text your message here"
Expand All @@ -104,21 +133,22 @@ export const Waku: React.FunctionComponent<WakuProps> = (props) => {
);
};

function useContentTopic(globalContentTopic: string) {
const [contentTopic, setContentTopic] = React.useState<string>(globalContentTopic);
function useTopic<T>(globalTopic: string): [string, (e: React.SyntheticEvent<T>) => void] {
const [topic, setTopic] = React.useState<string>(globalTopic);

React.useEffect(() => {
setContentTopic(globalContentTopic);
}, [globalContentTopic]);
setTopic(globalTopic);
}, [globalTopic]);

const onContentTopicChange = (e: React.SyntheticEvent<HTMLInputElement>) => {
setContentTopic(e.currentTarget.value || "");
const onTopicChange = (e: React.SyntheticEvent<T>) => {
const target = e.currentTarget as any;
setTopic(target?.value || "");
};

return {
contentTopic,
onContentTopicChange,
};
return [
topic,
onTopicChange,
];
}

function useMessage() {
Expand Down
6 changes: 5 additions & 1 deletion src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default function Home() {
messages,
debugInfo,
contentTopic,
onContentTopicChange
onContentTopicChange,
pubsubTopic,
onPubsubTopicChange
} = useWaku();

return (
Expand All @@ -23,6 +25,8 @@ export default function Home() {
messages={messages}
activeContentTopic={contentTopic}
onActiveContentTopicChange={onContentTopicChange}
activePubsubTopic={pubsubTopic}
onActivePubsubTopicChange={onPubsubTopicChange}
/>
</main>
);
Expand Down
12 changes: 11 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
export const CONTENT_TOPIC = "/toy-chat/2/luzhou/proto";
export const PUBSUB_TOPIC = "/waku/2/default-waku/proto";
export const PUBSUB_TOPIC = "/waku/2/rs/1/0";
export const SUPPORTED_PUBSUB_TOPICS = [
"/waku/2/rs/1/0",
"/waku/2/rs/1/1",
"/waku/2/rs/1/2",
"/waku/2/rs/1/3",
"/waku/2/rs/1/4",
"/waku/2/rs/1/5",
"/waku/2/rs/1/6",
"/waku/2/rs/1/7",
];

export const SIGNATURE_MESSAGE =
"The signature of this message will be used to generate your RLN credentials. Anyone accessing it may send messages on your behalf, please only share with the RLN dApp";
24 changes: 17 additions & 7 deletions src/hooks/useWaku.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { CONTENT_TOPIC } from "@/constants";
import { CONTENT_TOPIC, PUBSUB_TOPIC } from "@/constants";
import { DebugInfo, Message, waku } from "@/services/waku";

export type MessageContent = {
Expand All @@ -10,6 +10,7 @@ export type MessageContent = {

export const useWaku = () => {
const [contentTopic, setContentTopic] = React.useState<string>(CONTENT_TOPIC);
const [pubsubTopic, setPubsubTopic] = React.useState<string>(PUBSUB_TOPIC);
const [messages, setMessages] = React.useState<Map<string, MessageContent>>(new Map());
const [debugInfo, setDebugInfo] = React.useState<undefined | DebugInfo>();

Expand Down Expand Up @@ -58,10 +59,10 @@ export const useWaku = () => {
};
}, [debugInfo, setDebugInfo]);

const onSend = React.useCallback(
const onSend =
async (nick: string, text: string) => {
const timestamp = Date.now();
await waku.relay.send({
await waku.relay.send(pubsubTopic, {
version: 0,
timestamp,
contentTopic,
Expand All @@ -79,9 +80,7 @@ export const useWaku = () => {
next.set(id, { nick, timestamp, text });
return next;
});
},
[setMessages]
);
};

const onContentTopicChange = async (nextContentTopic: string) => {
if (nextContentTopic === contentTopic) {
Expand All @@ -91,11 +90,22 @@ export const useWaku = () => {
setContentTopic(nextContentTopic);
};

const onPubsubTopicChange = async (nextPubsubTopic: string) => {
if (nextPubsubTopic === pubsubTopic) {
return;
}

setPubsubTopic(nextPubsubTopic);
waku.relay.changeActivePubsubTopic(nextPubsubTopic);
};

return {
onSend,
debugInfo,
contentTopic,
onContentTopicChange,
messages: Array.from(messages.values())
pubsubTopic,
onPubsubTopicChange,
messages: Array.from(messages.values()),
};
};
17 changes: 11 additions & 6 deletions src/services/waku.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PUBSUB_TOPIC } from "@/constants";
import { PUBSUB_TOPIC, SUPPORTED_PUBSUB_TOPICS } from "@/constants";
import { http } from "@/utils/http";

export type Message = {
Expand All @@ -17,6 +17,7 @@ const RELAY = "/relay/v1";
const buildURL = (endpoint: string) => `${LOCAL_NODE}${endpoint}`;

class Relay {
private activePubsubTopic = SUPPORTED_PUBSUB_TOPICS[0];
private subscribing = false;
private readonly subscriptionsEmitter = new EventTarget();
// only one content topic subscriptions is possible now
Expand All @@ -43,13 +44,13 @@ class Relay {

this.subscribing = true;
try {
await http.post(buildURL(`${RELAY}/subscriptions`), [PUBSUB_TOPIC]);
await http.post(buildURL(`${RELAY}/subscriptions`), SUPPORTED_PUBSUB_TOPICS);

this.subscriptionRoutine = window.setInterval(async () => {
await this.fetchMessages();
}, 5 * SECOND);
} catch (error) {
console.error(`Failed to subscribe node ${PUBSUB_TOPIC}:`, error);
console.error(`Failed to subscribe node any of ${SUPPORTED_PUBSUB_TOPICS}:`, error);
}
this.subscribing = false;
}
Expand All @@ -71,7 +72,7 @@ class Relay {

private async fetchMessages(): Promise<void> {
const response = await http.get(
buildURL(`${RELAY}/messages/${encodeURIComponent(PUBSUB_TOPIC)}`)
buildURL(`${RELAY}/messages/${encodeURIComponent(this.activePubsubTopic)}`)
);
const body: Message[] = await response.json();

Expand Down Expand Up @@ -102,8 +103,12 @@ class Relay {
});
}

public async send(message: Message): Promise<void> {
await http.post(buildURL(`${RELAY}/messages/${encodeURIComponent(PUBSUB_TOPIC)}`), message);
public changeActivePubsubTopic(pubsubTopic: string) {
this.activePubsubTopic = pubsubTopic;
}

public async send(pubsubTopic: string, message: Message): Promise<void> {
await http.post(buildURL(`${RELAY}/messages/${encodeURIComponent(pubsubTopic)}`), message);
}
}

Expand Down

0 comments on commit 48d0261

Please sign in to comment.