Skip to content

Commit

Permalink
Implement SSE globally
Browse files Browse the repository at this point in the history
  • Loading branch information
samaradel committed Feb 6, 2025
1 parent d843d91 commit ee32c46
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
24 changes: 2 additions & 22 deletions client/src/layouts/default/AppBar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ import { useUserStore } from "@/store/UserStore";
import userService from "@/services/userService";
const drawer = ref(false);
const notifications = ref([]);
const isActive = ref(0);
const toast = ref(null);
const store = useUserStore();
const { user, isLoading, getUserInfo } = storeToRefs(store);
const { user, isLoading, notifications } = storeToRefs(store);
const navItems = ref([
{ title: "Home", path: "/" },
{ title: "Virtual Machines", path: "/vm" },
Expand Down Expand Up @@ -184,28 +184,8 @@ const seen = (id) => {
});
};
async function runSSE() {
const token = localStorage.getItem("token");
if (!token) return;
const response = await fetch("http://localhost:3000/v1/notification/stream", {
method: "GET",
headers: { Authorization: `Bearer ${token}` },
});
if (!response.ok) {
console.error("Failed to connect to SSE endpoint");
return;
}
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let finished = false;
while (!finished) {
const { done } = await reader.read();
if (done) break;
}
}
onMounted(async () => {
if (!user.value) await getUserInfo;
getNotifications();
runSSE();
});
</script>

Expand Down
8 changes: 6 additions & 2 deletions client/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import "mosha-vue-toastify/dist/style.css";
import mitt from "mitt";

const pinia = createPinia();

import { useUserStore } from "./store/UserStore";
// Plugins
import { registerPlugins } from "@/plugins";

Expand All @@ -31,4 +31,8 @@ app.component("No-Navbar-Layout", NoNavbar);

app.provide("emitter", emitter);

app.use(pinia).use(moshaToast).mount("#app");
app.use(pinia);
const store = useUserStore();
store.startSSE();

app.use(moshaToast).mount("#app");
6 changes: 6 additions & 0 deletions client/src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ export default {
});
},

async SSE() {
return await authClient().get("/notification/stream", {
responseType: "stream",
});
},

// user
async getUser() {
return await authClient().get("/user");
Expand Down
19 changes: 16 additions & 3 deletions client/src/store/UserStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ export const useUserStore = defineStore("userStore", {
user: null,
newUser: null,
isLoaded: false,
notifications: [],
maintenance: false,
next_launch: false,
next_launch_admin: false,
isAuthenticated: localStorage.getItem("token"),
}),
actions: {
async login(email, password) {
try {
const res = await userService.signIn(email, password);
const { access_token } = res.data.data;
localStorage.setItem("token", access_token);
this.isLoaded = true;
return res;
} catch (error) {
return error;
if (error) throw error;
}
},
async getUserInfo() {
Expand All @@ -31,14 +34,24 @@ export const useUserStore = defineStore("userStore", {
if (error.response.status == 401) {
localStorage.removeItem("token");
router.push("/login");
return error
return error;
}
return error
return error;
} finally {
this.isLoaded = true;
}
},

async startSSE() {
try {
await userService.SSE((notification) => {
this.notifications.push(notification);
});
} catch (error) {
console.error("Error starting SSE:", error);
}
},

async getNextLaunch() {
try {
const response = await userService.nextLaunch();
Expand Down

0 comments on commit ee32c46

Please sign in to comment.