|
| 1 | +import { useEffect, useState } from "react"; |
| 2 | + |
| 3 | +import { |
| 4 | + useCreateEventStream, |
| 5 | + useGetEventStream, |
| 6 | +} from "@squonk/account-server-client/event-stream"; |
| 7 | + |
| 8 | +import { useSnackbar } from "notistack"; |
| 9 | +import * as protobuf from "protobufjs"; |
| 10 | + |
| 11 | +export const EventStream = () => { |
| 12 | + const [eventStreamId, setEventStreamId] = useState<number | null>(null); |
| 13 | + const [location, setLocation] = useState<string | null>(null); |
| 14 | + const [websocket, setWebsocket] = useState<WebSocket | null>(null); |
| 15 | + const [protoRoot, setProtoRoot] = useState<protobuf.Root | null>(null); |
| 16 | + const { enqueueSnackbar } = useSnackbar(); |
| 17 | + const { data, error } = useGetEventStream(); |
| 18 | + |
| 19 | + useEffect(() => { |
| 20 | + // Initialize protobuf root |
| 21 | + const root = new protobuf.Root(); |
| 22 | + // You would typically load your proto definitions here |
| 23 | + // For example: root.load("path/to/proto/file.proto", { keepCase: true }) |
| 24 | + setProtoRoot(root); |
| 25 | + }, []); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + if (data) { |
| 29 | + setEventStreamId(data.id); |
| 30 | + setLocation(data.location); |
| 31 | + } |
| 32 | + }, [data]); |
| 33 | + |
| 34 | + const { mutate: createEventStream } = useCreateEventStream({ |
| 35 | + mutation: { |
| 36 | + onSuccess: (data) => { |
| 37 | + setEventStreamId(data.id); |
| 38 | + setLocation(data.location); |
| 39 | + }, |
| 40 | + }, |
| 41 | + }); |
| 42 | + |
| 43 | + useEffect(() => { |
| 44 | + if (error?.response?.status === 404) { |
| 45 | + createEventStream(); |
| 46 | + } |
| 47 | + }, [error, createEventStream]); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + if (location && protoRoot) { |
| 51 | + // Create WebSocket connection |
| 52 | + const ws = new WebSocket(location); |
| 53 | + |
| 54 | + ws.addEventListener("open", () => { |
| 55 | + enqueueSnackbar("Connected to event stream", { |
| 56 | + variant: "success", |
| 57 | + anchorOrigin: { horizontal: "right", vertical: "bottom" }, |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + ws.addEventListener("error", () => { |
| 62 | + enqueueSnackbar("Failed to connect to event stream", { |
| 63 | + variant: "error", |
| 64 | + anchorOrigin: { horizontal: "right", vertical: "bottom" }, |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + ws.addEventListener("close", () => { |
| 69 | + enqueueSnackbar("Disconnected from event stream", { |
| 70 | + variant: "warning", |
| 71 | + anchorOrigin: { horizontal: "right", vertical: "bottom" }, |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + ws.addEventListener("message", (event) => { |
| 76 | + enqueueSnackbar(`Received event: ${event.data}`, { |
| 77 | + variant: "info", |
| 78 | + anchorOrigin: { horizontal: "right", vertical: "bottom" }, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + setWebsocket(ws); |
| 83 | + |
| 84 | + return () => { |
| 85 | + ws.close(); |
| 86 | + }; |
| 87 | + } |
| 88 | + }, [location, protoRoot, enqueueSnackbar]); |
| 89 | + |
| 90 | + console.log(eventStreamId); |
| 91 | + console.log(websocket); |
| 92 | + |
| 93 | + return null; |
| 94 | +}; |
0 commit comments