Skip to content

Commit 41f3a5d

Browse files
committed
feat: enhance Socket.IO configuration and improve WebSocket handling
- Updated the Docker Compose configuration with additional comments for clarity on WebSocket and reverse-proxy setup. - Introduced a new environment variable handling for Socket.IO polling behavior in nuxt.config.ts, allowing for dynamic configuration based on the environment. - Refactored the Socket.IO client options to utilize the new polling configuration, improving flexibility in transport methods. - Enhanced logging to provide better insights into the Socket.IO polling configuration during runtime.
1 parent 035fd72 commit 41f3a5d

3 files changed

Lines changed: 39 additions & 5 deletions

File tree

apps/web/nuxt.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@ const SESAME_APP_API_URL = process.env.SESAME_APP_API_URL || 'http://127.0.0.1:4
1010
const SESAME_ALLOWED_HOSTS = process.env.SESAME_ALLOWED_HOSTS ? process.env.SESAME_ALLOWED_HOSTS.split(',') : []
1111
const API_PROXY_TARGET = SESAME_APP_API_URL.replace(/\/$/, '')
1212
const IS_DEV = process.env.NODE_ENV === 'development'
13+
/** Par défaut : polling seul en dev (proxy WS cassé), WebSocket en prod. Surcharge : SESAME_SOCKET_IO_POLLING_ONLY=0|1 */
14+
const SOCKET_IO_POLLING_ONLY =
15+
process.env.SESAME_SOCKET_IO_POLLING_ONLY !== undefined
16+
? /^(1|true|yes|on)$/i.test(process.env.SESAME_SOCKET_IO_POLLING_ONLY)
17+
: IS_DEV
1318

1419
if (SESAME_ALLOWED_HOSTS.length === 0 && !/localhost/.test(SESAME_APP_API_URL) && IS_DEV) {
1520
SESAME_ALLOWED_HOSTS.push(new URL(SESAME_APP_API_URL).hostname)
1621
}
1722

1823
consola.info(`[Nuxt] SESAME_APP_API_URL: ${SESAME_APP_API_URL}`)
24+
consola.info(`[Nuxt] Socket.IO polling only: ${SOCKET_IO_POLLING_ONLY}`)
1925
consola.info(`[Nuxt] SESAME_ALLOWED_HOSTS: ${SESAME_ALLOWED_HOSTS}`)
2026

2127
let SESAME_APP_DARK_MODE: 'auto' | boolean = false
@@ -78,6 +84,7 @@ export default defineNuxtConfig({
7884
runtimeConfig: {
7985
public: {
8086
release: process.env.npm_package_name + '@' + process.env.npm_package_version,
87+
socketIoPollingOnly: SOCKET_IO_POLLING_ONLY,
8188
sentry: {
8289
dsn: process.env.SESAME_SENTRY_DSN,
8390
},
Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,43 @@
11
import type { ManagerOptions, SocketOptions } from 'socket.io-client'
2+
import { tryUseNuxtApp, useRuntimeConfig } from '#app'
23

3-
/** Même préfixe que le proxy `/api/**` → API (cf. `nuxt.config.ts` routeRules). */
4+
/** Même préfixe que le proxy `/api/**` → API (cf. `nuxt.config.ts`). */
45
export const SOCKET_IO_PATH = '/api/socket.io'
56

7+
function readSocketIoPollingOnly(): boolean {
8+
const nuxtApp = tryUseNuxtApp()
9+
const fromApp = nuxtApp?.$config?.public?.socketIoPollingOnly
10+
if (fromApp !== undefined) {
11+
return Boolean(fromApp)
12+
}
13+
14+
if (import.meta.client) {
15+
const injected = (window as { __NUXT__?: { config?: { public?: { socketIoPollingOnly?: boolean } } } }).__NUXT__?.config
16+
?.public?.socketIoPollingOnly
17+
if (injected !== undefined) {
18+
return Boolean(injected)
19+
}
20+
}
21+
22+
return Boolean(useRuntimeConfig().public.socketIoPollingOnly)
23+
}
24+
625
/**
7-
* Dev : long-polling seul (le proxy Nuxt ne gère pas l'upgrade WS sur /api/socket.io).
8-
* Prod : WebSocket via le reverse-proxy, repli polling si besoin.
26+
* Transports Socket.IO.
27+
* Contrôlé par `SESAME_SOCKET_IO_POLLING_ONLY` (défaut : true en dev, false en prod).
928
*/
1029
export function socketIoTransports(): ('websocket' | 'polling')[] {
11-
return import.meta.dev ? ['polling'] : ['websocket', 'polling']
30+
return readSocketIoPollingOnly() ? ['polling'] : ['websocket', 'polling']
1231
}
1332

1433
export function buildSocketIoClientOptions(auth: { id: string; key: string }): Partial<ManagerOptions & SocketOptions> {
34+
const pollingOnly = readSocketIoPollingOnly()
35+
1536
return {
1637
path: SOCKET_IO_PATH,
1738
query: { id: String(auth.id), key: String(auth.key) },
1839
transports: socketIoTransports(),
19-
upgrade: !import.meta.dev,
40+
upgrade: !pollingOnly,
2041
reconnectionAttempts: 10,
2142
}
2243
}

docker-compose.prod.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
# docker network create reverse # si reverse-proxy externe
77
#
88
# Socket.IO : le client utilise `/api/socket.io` (même origine que le front).
9+
# apps/web/.env : ne pas définir SESAME_SOCKET_IO_POLLING_ONLY (ou =0) → WebSocket en prod.
910
# Reverse-proxy : router tout le trafic (HTTP + WebSocket) vers le port Nuxt (3000) ;
1011
# Nuxt proxifie `/api/**` vers l'API interne (SESAME_APP_API_URL=http://127.0.0.1:4000).
1112
#
13+
# Test WebSocket avant prod :
14+
# SESAME_SOCKET_IO_POLLING_ONLY=0 yarn workspace @libertech-fr/sesame-orchestrator_web build
15+
# node apps/web/.output/server/index.mjs
16+
# → panneau debug Socket.IO : transport "websocket"
17+
#
1218
# Démarrage :
1319
# docker compose -f docker-compose.prod.yml up -d
1420

0 commit comments

Comments
 (0)