From 4ce3e29f579d4d3ab46dca6f926fc047a37933f3 Mon Sep 17 00:00:00 2001 From: Neshura Date: Sat, 6 Jan 2024 04:31:40 +0100 Subject: [PATCH] Optimize Socket.io connections for fewer open requests at any one time --- src/lib/stores/socketStore.ts | 4 ++++ src/lib/stores/uptimeStore.ts | 4 ++++ src/routes/+layout.svelte | 14 +++++++++++++ src/routes/servers/+page.svelte | 34 ++++++++++++++------------------ src/routes/services/+page.svelte | 20 ++++++++++++++----- 5 files changed, 52 insertions(+), 24 deletions(-) create mode 100644 src/lib/stores/socketStore.ts create mode 100644 src/lib/stores/uptimeStore.ts diff --git a/src/lib/stores/socketStore.ts b/src/lib/stores/socketStore.ts new file mode 100644 index 0000000..303110e --- /dev/null +++ b/src/lib/stores/socketStore.ts @@ -0,0 +1,4 @@ +import { writable } from 'svelte/store'; +import {io} from "socket.io-client"; + +export let socketStore = writable(io('https://status.neshweb.net/')); \ No newline at end of file diff --git a/src/lib/stores/uptimeStore.ts b/src/lib/stores/uptimeStore.ts new file mode 100644 index 0000000..1dc9c47 --- /dev/null +++ b/src/lib/stores/uptimeStore.ts @@ -0,0 +1,4 @@ +import {type Writable, writable} from 'svelte/store'; +import type {Heartbeat} from "$lib/types/uptime-kuma-types"; + +export let uptimeStore: Writable> = writable(new Map()); \ No newline at end of file diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index b3c1733..d6f4917 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,6 +1,20 @@ + +
diff --git a/src/routes/servers/+page.svelte b/src/routes/servers/+page.svelte index ecc60f8..afea722 100644 --- a/src/routes/servers/+page.svelte +++ b/src/routes/servers/+page.svelte @@ -5,6 +5,8 @@ import { io } from 'socket.io-client'; import type { Heartbeat } from '$lib/types/uptime-kuma-types'; import ServerCard from '$lib/components/ServerCard.svelte'; + import { socketStore } from '$lib/stores/socketStore'; + import {uptimeStore} from "$lib/stores/uptimeStore"; let { data }: { data: { promise: Promise } } = $props(); @@ -18,33 +20,25 @@ let icons: readonly string[] = $state.frozen([]); - let monitorList: Map = $state(new Map()); + let monitorList = $state($uptimeStore); - //let token = $props(); + console.log(monitorList); + + let socket = $socketStore; $effect(() => { if (token) { - const socket = io('https://status.neshweb.net/'); - + if (!socket.connected) { + socket.connect(); + } + else { + console.log("already connected"); + } socket.on('connect', () => { + console.log("logging in") socket.emit('loginByToken', token, () => {}); }); - socket.on('importantHeartbeatList', (_, data) => { - let recent = data[data.length - 1]; - let monitor: Heartbeat = { - monitorID: recent.monitor_id, - status: recent.status, - time: recent.time, - msg: recent.msg, - ping: recent.ping, - important: recent.important, - duration: recent.duration - }; - monitorList.set(monitor.monitorID, monitor); - monitorList = new Map(monitorList.entries()); - }); - socket.on('heartbeatList', (_, data) => { let recent = data[data.length - 1]; let monitor: Heartbeat = { @@ -58,11 +52,13 @@ }; monitorList.set(monitor.monitorID, monitor); monitorList = new Map(monitorList.entries()); + $uptimeStore = monitorList; }); socket.on('heartbeat', (data) => { monitorList.set(data.monitorID, data); monitorList = new Map(monitorList.entries()); + $uptimeStore = monitorList; }); } }); diff --git a/src/routes/services/+page.svelte b/src/routes/services/+page.svelte index b895680..9a3afc6 100644 --- a/src/routes/services/+page.svelte +++ b/src/routes/services/+page.svelte @@ -5,6 +5,9 @@ import type { Service } from '$lib/types/data-types'; import { io } from 'socket.io-client'; import type { Heartbeat } from '$lib/types/uptime-kuma-types'; + import {socketStore} from "$lib/stores/socketStore"; + import {onDestroy} from "svelte"; + import {uptimeStore} from "$lib/stores/uptimeStore"; let { data }: { data: { promise: Promise } } = $props(); @@ -18,19 +21,24 @@ let icons: readonly string[] = $state.frozen([]); - let monitorList: Map = $state(new Map()); + let monitorList = $state($uptimeStore); - //let token = $props(); + let socket = $socketStore; $effect(() => { if (token) { - const socket = io('https://status.neshweb.net/'); - + if (!socket.connected) { + socket.connect(); + } + else { + console.log("connected") + } socket.on('connect', () => { + console.log("login") socket.emit('loginByToken', token, () => {}); }); - socket.on('heartbeatList', (idString: string, data) => { + socket.on('heartbeatList', (_: string, data) => { let recent = data[data.length - 1]; let monitor: Heartbeat = { monitorID: recent.monitor_id, @@ -43,11 +51,13 @@ }; monitorList.set(monitor.monitorID, monitor); monitorList = new Map(monitorList.entries()); + $uptimeStore = monitorList; }); socket.on('heartbeat', (data) => { monitorList.set(data.monitorID, data); monitorList = new Map(monitorList.entries()); + $uptimeStore = monitorList; }); } });