From 53cb864863df1291b349b8541a413f41632d40ea Mon Sep 17 00:00:00 2001 From: Mekan1206 Date: Sat, 15 Aug 2026 20:51:47 +0500 Subject: [PATCH] WIP --- .env.example | 8 +++++--- src/utils/echo.js | 30 +++++++++++++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/.env.example b/.env.example index b25ecb4..d7c0749 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,8 @@ VITE_API_URL=https://mm.com.tm/api/v1/ VITE_API_TOKEN=hello-mf-s VITE_REVERB_APP_KEY= -VITE_REVERB_HOST=127.0.0.1 -VITE_REVERB_PORT=8080 -VITE_REVERB_SCHEME=http +# Production (nginx proxies /app → Reverb on 127.0.0.1:8080): +VITE_REVERB_HOST=mm.com.tm +VITE_REVERB_PORT=443 +VITE_REVERB_SCHEME=https +# Local: VITE_REVERB_HOST=127.0.0.1 VITE_REVERB_PORT=8080 VITE_REVERB_SCHEME=http diff --git a/src/utils/echo.js b/src/utils/echo.js index 591ddea..298f42e 100644 --- a/src/utils/echo.js +++ b/src/utils/echo.js @@ -25,6 +25,32 @@ function apiOrigin() { return url.replace(/\/?api\/v1\/?$/, ""); } +function isLoopbackHost(host) { + return !host || host === "127.0.0.1" || host === "localhost" || host === "0.0.0.0"; +} + +function reverbEndpoint() { + const envHost = import.meta.env.VITE_REVERB_HOST; + const envScheme = import.meta.env.VITE_REVERB_SCHEME; + const envPort = import.meta.env.VITE_REVERB_PORT; + + if (import.meta.env.PROD && isLoopbackHost(envHost)) { + const https = window.location.protocol === "https:"; + return { + host: window.location.hostname, + scheme: https ? "https" : "http", + port: https ? 443 : 80, + }; + } + + const scheme = envScheme || "http"; + return { + host: envHost || "127.0.0.1", + scheme, + port: Number(envPort || (scheme === "https" ? 443 : 8080)), + }; +} + export function getChatEcho() { const key = import.meta.env.VITE_REVERB_APP_KEY; const token = getToken(); @@ -44,9 +70,7 @@ export function getChatEcho() { window.Pusher = Pusher; - const scheme = import.meta.env.VITE_REVERB_SCHEME || "http"; - const port = Number(import.meta.env.VITE_REVERB_PORT || (scheme === "https" ? 443 : 8080)); - const host = import.meta.env.VITE_REVERB_HOST || "127.0.0.1"; + const { host, scheme, port } = reverbEndpoint(); echoToken = token; echoClient = new Echo({