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:
@@ -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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user