Refactor chat configuration: update .env.example for Reverb settings, streamline Pusher configuration in SupportChatController, and enhance chat.js to dynamically determine websocket endpoint based on host. This improves flexibility for local and production environments.

This commit is contained in:
Mekan1206 committed 2026-08-15 20:50:09 +05:00
1 parent 3a415edb6e
commit f68db67465
4 files changed
+92 -13

No files matched your search

+7 -1
View File
@@ -50,7 +50,9 @@ AWS_USE_PATH_STYLE_ENDPOINT=false
# Storefront widget + Nova inbox 1:1 target (users.id) # Storefront widget + Nova inbox 1:1 target (users.id)
CHAT_SUPPORT_USER_ID= CHAT_SUPPORT_USER_ID=
# Laravel Reverb (`php artisan reverb:start`) # Laravel Reverb (`php artisan reverb:start` / supervisor).
# PHP broadcasts to this host. Browsers use nginx at wss://mm.com.tm/app.
# Local:
REVERB_APP_ID= REVERB_APP_ID=
REVERB_APP_KEY= REVERB_APP_KEY=
REVERB_APP_SECRET= REVERB_APP_SECRET=
@@ -59,6 +61,10 @@ REVERB_PORT=8080
REVERB_SCHEME=http REVERB_SCHEME=http
REVERB_SERVER_HOST=0.0.0.0 REVERB_SERVER_HOST=0.0.0.0
REVERB_SERVER_PORT=8080 REVERB_SERVER_PORT=8080
# Production (keep SERVER_* on 8080; PHP can still use 127.0.0.1:8080):
# REVERB_HOST=127.0.0.1
# REVERB_PORT=8080
# REVERB_SCHEME=http
VITE_REVERB_APP_KEY="${REVERB_APP_KEY}" VITE_REVERB_APP_KEY="${REVERB_APP_KEY}"
VITE_REVERB_HOST="${REVERB_HOST}" VITE_REVERB_HOST="${REVERB_HOST}"
@@ -31,12 +31,7 @@ public function index(): View
$key ? $this->panelAsset('js/echo.iife.js') : null, $key ? $this->panelAsset('js/echo.iife.js') : null,
$this->panelAsset('js/chat.js'), $this->panelAsset('js/chat.js'),
])), ])),
'pusher' => [ 'pusher' => $this->chatPusherConfig($key),
'key' => $key,
'host' => config('broadcasting.connections.reverb.options.host'),
'port' => (int) config('broadcasting.connections.reverb.options.port', 8080),
'scheme' => config('broadcasting.connections.reverb.options.scheme', 'http'),
],
]); ]);
} }
@@ -110,4 +105,36 @@ private function panelAsset(string $relative): string
return (string) file_get_contents($path); return (string) file_get_contents($path);
} }
/**
* Browser websocket host. Reverb itself stays on 127.0.0.1:8080;
* production browsers connect through nginx at wss://mm.com.tm/app.
*
* @return array{key: mixed, host: string, port: int, scheme: string}
*/
private function chatPusherConfig(mixed $key): array
{
$requestHost = request()->getHost();
$public = ! in_array($requestHost, ['127.0.0.1', 'localhost', '::1', '0.0.0.0'], true);
if ($public) {
$https = request()->secure()
|| request()->header('X-Forwarded-Proto') === 'https'
|| str_contains($requestHost, '.');
return [
'key' => $key,
'host' => $requestHost,
'port' => $https ? 443 : 80,
'scheme' => $https ? 'https' : 'http',
];
}
return [
'key' => $key,
'host' => (string) config('broadcasting.connections.reverb.options.host'),
'port' => (int) config('broadcasting.connections.reverb.options.port', 8080),
'scheme' => (string) config('broadcasting.connections.reverb.options.scheme', 'http'),
];
}
} }
+25
View File
@@ -0,0 +1,25 @@
# Laravel Reverb websocket proxy for mm.com.tm
#
# Paste this location into the existing HTTPS server { } for mm.com.tm,
# BEFORE any frontend SPA catch-all.
#
# Reverb (supervisor) stays on 127.0.0.1:8080.
# Browsers connect to wss://mm.com.tm/app/...
location /app {
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 86400s;
proxy_buffering off;
proxy_pass http://127.0.0.1:8080;
}
+27 -6
View File
@@ -266,6 +266,28 @@
state.echo.private(`chat.${conversationId}`).listen(".message.sent", upsertIncoming); state.echo.private(`chat.${conversationId}`).listen(".message.sent", upsertIncoming);
} }
function isLoopbackHost(host) {
return !host || host === "127.0.0.1" || host === "localhost" || host === "0.0.0.0" || host === "::1";
}
function echoEndpoint(pusher) {
const pageHost = window.location.hostname;
if (!isLoopbackHost(pageHost)) {
const https = window.location.protocol === "https:";
return {
host: pageHost,
port: https ? 443 : 80,
forceTLS: https,
};
}
return {
host: pusher.host || "127.0.0.1",
port: pusher.port || 8080,
forceTLS: (pusher.scheme || "http") === "https",
};
}
function bootEcho() { function bootEcho() {
const pusher = config.pusher || {}; const pusher = config.pusher || {};
const EchoClass = window.Echo; const EchoClass = window.Echo;
@@ -273,17 +295,16 @@
return; return;
} }
const scheme = pusher.scheme || "https"; const endpoint = echoEndpoint(pusher);
const forceTLS = scheme === "https";
try { try {
state.echo = new EchoClass({ state.echo = new EchoClass({
broadcaster: "reverb", broadcaster: "reverb",
key: pusher.key, key: pusher.key,
wsHost: pusher.host || undefined, wsHost: endpoint.host,
wsPort: pusher.port || undefined, wsPort: endpoint.port,
wssPort: pusher.port || undefined, wssPort: endpoint.port,
forceTLS, forceTLS: endpoint.forceTLS,
enabledTransports: ["ws", "wss"], enabledTransports: ["ws", "wss"],
disableStats: true, disableStats: true,
cluster: "mt1", cluster: "mt1",