Skip to content

Commit

Permalink
refresh token on ws error
Browse files Browse the repository at this point in the history
  • Loading branch information
aabassiouni committed May 25, 2024
1 parent 4b07e1d commit 228da76
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
20 changes: 5 additions & 15 deletions apps/web/src/components/realtime-switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,27 @@
import { Switch } from "@/components/ui/switch";
import { useWebSocket } from "@/lib/hooks/useWebsocket";
import { useRealtimeCountActions } from "@/lib/store";
import { useAuth } from "@clerk/nextjs";
import { useParams, useRouter } from "next/navigation";
import React, { useEffect, useState } from "react";
import React, { useState } from "react";

export function RealtimeSwitch() {
const router = useRouter();
const params = useParams<{ waitlist: string }>();
const { incrementRealtimeCount, resetRealtimeCount } = useRealtimeCountActions();

const [enabled, setEnabled] = useState(false);
const [token, setToken] = useState<string | null>(null);
const { getToken } = useAuth();

const { ws, reconnect } = useWebSocket(`${process.env.NEXT_PUBLIC_REALTIME_URL}/${params.waitlist}/ws`, {
onMessage: () => {
incrementRealtimeCount();
},
onClose: () => setEnabled(false),
onClose: () => {
setEnabled(false);
},
enabled,
authToken: token ?? undefined,
authToken: true,
});

useEffect(() => {
async function fetchToken() {
const token = await getToken();
setToken(token);
}

fetchToken();
}, []);

return (
<div className="flex items-center gap-2">
<p className="items-center font-medium text-lg tracking-tight">Enable Realtime</p>
Expand Down
20 changes: 18 additions & 2 deletions apps/web/src/lib/hooks/useWebsocket.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useAuth } from "@clerk/nextjs";
import { useCallback, useEffect, useState } from "react";

export const useWebSocket = (
Expand All @@ -6,23 +7,36 @@ export const useWebSocket = (
onMessage: (e: MessageEvent) => void;
onClose?: (e: CloseEvent) => void;
enabled?: boolean;
authToken?: string;
authToken?: boolean;
},
) => {
const [ws, setWs] = useState<WebSocket | null>(null);
const [key, setKey] = useState(0);
const [token, setToken] = useState<string | null>(null);
const { getToken } = useAuth();

const link = new URL(url);

if (opts.authToken) link.searchParams.append("token", opts.authToken);
async function fetchToken() {
setToken(await getToken());
}

if (opts.authToken) {
fetchToken();
}

const reconnect = useCallback(() => {
setKey((prev) => prev + 1);
}, []);

useEffect(() => {
if (opts.enabled) {
if (token) {
link.searchParams.set("token", token);
}

const ws = new WebSocket(link);

ws.onopen = () => {
console.log("[Connected to realtime]");
};
Expand All @@ -33,6 +47,8 @@ export const useWebSocket = (
ws.onmessage = opts.onMessage;
ws.onerror = (e) => {
console.error("[Realtime error]", e);
console.log("refreshing token");
fetchToken();
};

setWs(ws);
Expand Down

0 comments on commit 228da76

Please sign in to comment.