Skip to content

Commit d2d2cba

Browse files
authored
Merge pull request #57 from dscsnu/StreamListener
Stream listener
2 parents 516f731 + 665e810 commit d2d2cba

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed

frontend/package-lock.json

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@sveltejs/kit": "^2.16.0",
1717
"@sveltejs/vite-plugin-svelte": "^5.0.0",
1818
"@tailwindcss/vite": "^4.0.0",
19+
"@types/event-source-polyfill": "^1.0.5",
1920
"svelte": "^5.0.0",
2021
"svelte-check": "^4.0.0",
2122
"tailwindcss": "^4.0.0",
@@ -25,6 +26,7 @@
2526
"dependencies": {
2627
"@supabase/ssr": "^0.6.1",
2728
"@supabase/supabase-js": "^2.49.4",
29+
"event-source-polyfill": "^1.0.31",
2830
"nanoid": "^5.1.5",
2931
"tailwind-merge": "^3.2.0"
3032
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<script lang="ts">
2+
import { PUBLIC_BACKEND_URL, PUBLIC_STREAM_URL } from "$env/static/public";
3+
import { JwtTokenStore } from "$lib/stores/JwtTokenStore";
4+
import { TeamStore } from "$lib/stores/TeamStore";
5+
import { EventSourcePolyfill } from "event-source-polyfill";
6+
import { onMount } from "svelte";
7+
import { get } from "svelte/store";
8+
9+
let eventSource: EventSourcePolyfill | null = $state(null);
10+
11+
const cleanup = () => {
12+
if (eventSource) {
13+
eventSource.close();
14+
eventSource = null;
15+
}
16+
}
17+
18+
const connect = () => {
19+
console.log('Attempting Connection');
20+
const cleanedUrl = PUBLIC_BACKEND_URL.replace(/\/+$/, '');
21+
const cleanedEndpoint = PUBLIC_STREAM_URL.replace(/^\/+/, '');
22+
const jwt = get(JwtTokenStore);
23+
24+
eventSource = new EventSourcePolyfill(`${cleanedUrl}/${cleanedEndpoint}`, {
25+
headers: {
26+
'Authorization': `Bearer ${jwt}`
27+
}
28+
});
29+
30+
eventSource.onmessage = (event) => {
31+
alert(event.data);
32+
}
33+
34+
eventSource.onerror = (error) => {
35+
console.error(`Stream error: ${error}`);
36+
cleanup();
37+
}
38+
}
39+
40+
onMount(() => {
41+
const unsubscribe = TeamStore.subscribe(current => {
42+
if (current) {
43+
connect();
44+
} else {
45+
cleanup();
46+
}
47+
})
48+
49+
return () => unsubscribe();
50+
})
51+
</script>

frontend/src/routes/+layout.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import Toast from "$lib/components/Toast.svelte";
1212
import { LoadingStore } from "$lib/stores/LoadingStore";
1313
import Loading from "$lib/components/Loading.svelte";
14+
import StreamListener from "$lib/components/StreamListener.svelte";
1415
1516
let { data, children } = $props();
1617
let { supabase, session, user } = $derived(data);
@@ -71,3 +72,6 @@
7172
{#if $LoadingStore}
7273
<Loading />
7374
{/if}
75+
76+
77+
<StreamListener />

frontend/src/routes/new/+page.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
2828
LoadingStore.set(true);
2929
try {
30-
const response = await fetchWithAuth('api/createteam', {
30+
const response = await fetchWithAuth('api/team/create', {
3131
method: 'POST',
3232
headers: {
3333
'Content-Type': 'application/json',
@@ -84,7 +84,7 @@
8484
8585
LoadingStore.set(true)
8686
try {
87-
const response = await fetchWithAuth('api/updateteam', {
87+
const response = await fetchWithAuth('api/team/update', {
8888
method: 'POST',
8989
headers: {
9090
'Content-Type': 'application/json',

0 commit comments

Comments
 (0)