Skip to content

Commit

Permalink
feat!: migrate to websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
cstenglein committed Sep 24, 2024
1 parent bda50c6 commit e86fbd7
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 20 deletions.
8 changes: 3 additions & 5 deletions backend-mock/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require("dotenv").config();
const app = express();
const server = createServer(app);

const wss = new WebSocket.Server({ server, path: "/websocket" });
const wss = new WebSocket.Server({ server, path: "/ws" });

app.use(
cors({ credentials: true, origin: "http://localhost:3000" }),
Expand All @@ -38,17 +38,15 @@ const PORT = 8000;

server.listen(PORT, () => {
console.info(`Server listening on http://localhost:${PORT}`);
console.info(
`WebSocket server is running on ws://localhost:${PORT}/websocket`,
);
console.info(`WebSocket server is running on ws://localhost:${PORT}/ws`);
});

/**
* Main WebSocket Handler
*/

wss.on("connection", (ws) => {
console.info("WebSocket connection established on /websocket");
console.info("WebSocket connection established on /ws");

// Handle incoming messages
ws.on("message", (message) => {
Expand Down
2 changes: 1 addition & 1 deletion backend-mock/lightning.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const router = express.Router();
const transactions = require("./transactions");
const util = require("./sse/util");

let WALLET_LOCKED = false;
let WALLET_LOCKED = true;

router.post("/add-invoice", (req, res) => {
console.info(
Expand Down
3 changes: 1 addition & 2 deletions src/context/ws-context.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { WEBSOCKET_URL } from "@/hooks/use-ws";
import { AppStatus } from "@/models/app-status";
import { App } from "@/models/app.model";
import { BtcInfo } from "@/models/btc-info";
Expand Down Expand Up @@ -69,8 +70,6 @@ export const WebSocketContext = createContext<WebSocketContextType>(
websocketContextDefault,
);

export const WEBSOCKET_URL = "ws://your-websocket-server-url"; // Replace with your WebSocket server URL

const WebSocketContextProvider: FC<PropsWithChildren> = (props) => {
const [systemInfo, setSystemInfo] = useState<SystemInfo>({
alias: "",
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/use-ws.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useCallback, useContext, useEffect } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "react-toastify";

const WEBSOCKET_URL = "/websocket";
export const WEBSOCKET_URL = "/ws";

function useWebSocket() {
const { t } = useTranslation();
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const Home: FC = () => {
}
} catch (err: any) {
if (err.response.status === HttpStatusCode.Locked) {
setWalletLocked(false);
setWalletLocked(true);
} else {
setTxError(checkError(err));
}
Expand Down
18 changes: 9 additions & 9 deletions src/utils/test-utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import {
AppContextType,
} from "@/context/app-context";
import {
SSEContext,
sseContextDefault,
SSEContextType,
} from "@/context/sse-context";
WebSocketContext,
websocketContextDefault,
WebSocketContextType,
} from "@/context/ws-context";
import i18n from "@/i18n/test_config";
import { render, RenderOptions } from "@testing-library/react";
import { FC, PropsWithChildren, ReactElement } from "react";
import { I18nextProvider } from "react-i18next";
import { BrowserRouter } from "react-router-dom";

type Props = {
sseProps: SSEContextType;
sseProps: WebSocketContextType;
appProps: AppContextType;
};

Expand All @@ -26,9 +26,9 @@ const AllTheProviders: FC<PropsWithChildren<Props>> = ({
}) => {
return (
<BrowserRouter>
<SSEContext.Provider
<WebSocketContext.Provider
value={{
...sseContextDefault,
...websocketContextDefault,
...sseProps,
}}
>
Expand All @@ -40,7 +40,7 @@ const AllTheProviders: FC<PropsWithChildren<Props>> = ({
>
<I18nextProvider i18n={i18n}>{children}</I18nextProvider>
</AppContext.Provider>
</SSEContext.Provider>
</WebSocketContext.Provider>
</BrowserRouter>
);
};
Expand All @@ -49,7 +49,7 @@ const customRender = (
ui: ReactElement,
options?: Omit<RenderOptions, "wrapper"> & {
providerOptions?: {
sseProps?: Partial<SSEContextType>;
sseProps?: Partial<WebSocketContextType>;
appProps?: Partial<AppContextType>;
};
},
Expand Down
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default defineConfig({
changeOrigin: true,
secure: false,
},
"/websocket": {
"/ws": {
target: "ws://localhost:8000",
changeOrigin: true,
secure: false,
Expand Down

0 comments on commit e86fbd7

Please sign in to comment.