Add WebSocket URL to backend config API - frontend now reads from backend .env
All checks were successful
Build Frontend / Build Frontend (push) Successful in 9s

- Added websocket.url to backend config
- Updated /api/config/public endpoint to return WebSocket URL
- Frontend now fetches WS URL from backend config API
- Falls back to environment variable or auto-generated URL
- Add WS_URL to backend .env: wss://api.turbotrades.dev/ws
This commit is contained in:
2026-01-11 02:01:34 +00:00
parent 0bd3f60627
commit ae18e8b530
2 changed files with 21 additions and 3 deletions

View File

@@ -30,12 +30,24 @@ export const useWebSocketStore = defineStore("websocket", () => {
});
// Helper functions
const getWebSocketUrl = () => {
const getWebSocketUrl = async () => {
// Use environment variable or fallback
if (import.meta.env.VITE_WS_URL) {
return import.meta.env.VITE_WS_URL;
}
// Try to fetch from backend config API
try {
const apiUrl = import.meta.env.VITE_API_URL || "http://localhost:3000";
const response = await fetch(`${apiUrl}/api/config/public`);
const data = await response.json();
if (data.success && data.config.websocket?.url) {
return data.config.websocket.url;
}
} catch (error) {
console.warn("Failed to fetch WebSocket URL from config:", error);
}
// In production, use main API domain with /ws path
if (import.meta.env.PROD) {
const apiUrl =
@@ -76,7 +88,7 @@ export const useWebSocketStore = defineStore("websocket", () => {
};
// Actions
const connect = () => {
const connect = async () => {
if (ws.value?.readyState === WebSocket.OPEN || isConnecting.value) {
console.log("WebSocket already connected or connecting");
return;
@@ -86,7 +98,7 @@ export const useWebSocketStore = defineStore("websocket", () => {
clearReconnectTimeout();
try {
const wsUrl = getWebSocketUrl();
const wsUrl = await getWebSocketUrl();
console.log("Connecting to WebSocket:", wsUrl);
ws.value = new WebSocket(wsUrl);