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
2026-08-15 20:50:09 +05:00
parent 3a415edb6e
commit f68db67465
4 changed files with 92 additions and 13 deletions

View File

@@ -50,7 +50,9 @@ AWS_USE_PATH_STYLE_ENDPOINT=false
# Storefront widget + Nova inbox 1:1 target (users.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_KEY=
REVERB_APP_SECRET=
@@ -59,6 +61,10 @@ REVERB_PORT=8080
REVERB_SCHEME=http
REVERB_SERVER_HOST=0.0.0.0
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_HOST="${REVERB_HOST}"

View File

@@ -31,12 +31,7 @@ class SupportChatController extends Controller
$key ? $this->panelAsset('js/echo.iife.js') : null,
$this->panelAsset('js/chat.js'),
])),
'pusher' => [
'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'),
],
'pusher' => $this->chatPusherConfig($key),
]);
}
@@ -110,4 +105,36 @@ class SupportChatController extends Controller
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
nginx/reverb.conf Normal file
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;
}

View File

@@ -266,6 +266,28 @@
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() {
const pusher = config.pusher || {};
const EchoClass = window.Echo;
@@ -273,17 +295,16 @@
return;
}
const scheme = pusher.scheme || "https";
const forceTLS = scheme === "https";
const endpoint = echoEndpoint(pusher);
try {
state.echo = new EchoClass({
broadcaster: "reverb",
key: pusher.key,
wsHost: pusher.host || undefined,
wsPort: pusher.port || undefined,
wssPort: pusher.port || undefined,
forceTLS,
wsHost: endpoint.host,
wsPort: endpoint.port,
wssPort: endpoint.port,
forceTLS: endpoint.forceTLS,
enabledTransports: ["ws", "wss"],
disableStats: true,
cluster: "mt1",