feat: implement custom Next.js server with integrated WebSocket (POC)
- Create apps/nextjs/server.ts custom server integrating WebSocket via ws - Update nextjs scripts to use custom server for dev and start - Update root start script to remove separate ws server process - Update client tRPC URL to connect to port 3000 in dev - Update nginx.conf to route /websockets to port 3000 - Update Dockerfile and run.sh for integrated server - Disable standalone ws server dev script - Add benchmark-memory.sh script Co-authored-by: ajnart <49837342+ajnart@users.noreply.github.com>
This commit is contained in:
1 parent
000604bd02
commit
c8ae7f283a
11 files changed
+248
-31
No files matched your search
@@ -57,6 +57,7 @@ apps/tasks/tasks.cjs
|
||||
apps/tasks/tasks.css
|
||||
apps/websocket/wssServer.cjs
|
||||
apps/websocket/wssServer.css
|
||||
apps/nextjs/customServer.cjs
|
||||
apps/nextjs/.million/
|
||||
packages/cli/cli.cjs
|
||||
|
||||
|
||||
+1
-1
@@ -44,7 +44,7 @@ COPY --from=builder /app/apps/nextjs/next.config.ts .
|
||||
COPY --from=builder /app/apps/nextjs/package.json .
|
||||
|
||||
COPY --from=builder /app/apps/tasks/tasks.cjs ./apps/tasks/tasks.cjs
|
||||
COPY --from=builder /app/apps/websocket/wssServer.cjs ./apps/websocket/wssServer.cjs
|
||||
COPY --from=builder /app/apps/nextjs/customServer.cjs ./apps/nextjs/customServer.cjs
|
||||
COPY --from=builder /app/node_modules/better-sqlite3/build/Release/better_sqlite3.node /app/build/better_sqlite3.node
|
||||
|
||||
COPY --from=builder /app/packages/db/migrations ./db/migrations
|
||||
|
||||
@@ -4,12 +4,13 @@
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "pnpm with-env next build",
|
||||
"build": "pnpm with-env next build && pnpm build:custom-server",
|
||||
"build:custom-server": "esbuild server.ts --bundle --platform=node --outfile=customServer.cjs --external:bcrypt --external:@opentelemetry/api --external:deasync --external:cpu-features --loader:.html=text --loader:.scss=text --loader:.node=text",
|
||||
"clean": "git clean -xdf .next .turbo node_modules",
|
||||
"dev": "pnpm with-env next dev",
|
||||
"dev": "pnpm with-env tsx ./server.ts",
|
||||
"format": "prettier --check . --ignore-path ../../.gitignore",
|
||||
"lint": "eslint",
|
||||
"start": "pnpm with-env next start",
|
||||
"start": "pnpm with-env node customServer.cjs",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"with-env": "dotenv -e ../../.env --"
|
||||
},
|
||||
@@ -87,6 +88,7 @@
|
||||
"superjson": "2.2.6",
|
||||
"swagger-ui-react": "^5.31.2",
|
||||
"use-deep-compare-effect": "^1.8.1",
|
||||
"ws": "^8.19.0",
|
||||
"zod": "^4.2.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -99,10 +101,13 @@
|
||||
"@types/react": "19.2.14",
|
||||
"@types/react-dom": "19.2.3",
|
||||
"@types/swagger-ui-react": "^5.18.0",
|
||||
"@types/ws": "^8.18.1",
|
||||
"concurrently": "^9.2.1",
|
||||
"esbuild": "^0.27.3",
|
||||
"eslint": "^9.39.3",
|
||||
"node-loader": "^2.1.0",
|
||||
"prettier": "^3.8.1",
|
||||
"tsx": "4.20.4",
|
||||
"typescript": "^5.9.3"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import { createServer } from "http";
|
||||
import next from "next";
|
||||
import { parse } from "url";
|
||||
import { applyWSSHandler } from "@trpc/server/adapters/ws";
|
||||
import { WebSocketServer } from "ws";
|
||||
|
||||
import { appRouter, createTRPCContext } from "@homarr/api/websocket";
|
||||
import { getSessionFromToken, sessionTokenCookieName } from "@homarr/auth";
|
||||
import { parseCookies } from "@homarr/common";
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { db } from "@homarr/db";
|
||||
|
||||
const logger = createLogger({ module: "customServer" });
|
||||
|
||||
const dev = process.env.NODE_ENV !== "production";
|
||||
const hostname = process.env.HOSTNAME || "localhost";
|
||||
const port = parseInt(process.env.PORT || "3000", 10);
|
||||
|
||||
const app = next({ dev, hostname, port });
|
||||
const handle = app.getRequestHandler();
|
||||
|
||||
app.prepare().then(() => {
|
||||
const server = createServer((req, res) => {
|
||||
const parsedUrl = parse(req.url!, true);
|
||||
handle(req, res, parsedUrl);
|
||||
});
|
||||
|
||||
// Create WebSocket server in noServer mode so we handle upgrade manually
|
||||
const wss = new WebSocketServer({ noServer: true });
|
||||
|
||||
const wssHandler = applyWSSHandler({
|
||||
wss,
|
||||
router: appRouter,
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
createContext: async ({ req }) => {
|
||||
try {
|
||||
const headers = Object.entries(req.headers).map(
|
||||
([key, value]) => [key, typeof value === "string" ? value : value?.[0]] as [string, string],
|
||||
);
|
||||
const nextHeaders = new Headers(headers);
|
||||
|
||||
const store = parseCookies(nextHeaders.get("cookie") ?? "");
|
||||
const sessionToken = store[sessionTokenCookieName];
|
||||
|
||||
const session = await getSessionFromToken(db, sessionToken);
|
||||
|
||||
return createTRPCContext({
|
||||
headers: nextHeaders,
|
||||
session,
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(error);
|
||||
return createTRPCContext({
|
||||
headers: new Headers(),
|
||||
session: null,
|
||||
});
|
||||
}
|
||||
},
|
||||
keepAlive: {
|
||||
enabled: true,
|
||||
pingMs: 30000,
|
||||
pongWaitMs: 5000,
|
||||
},
|
||||
});
|
||||
|
||||
// Handle WebSocket upgrade requests on /websockets path
|
||||
server.on("upgrade", (req, socket, head) => {
|
||||
if (req.url?.startsWith("/websockets")) {
|
||||
wss.handleUpgrade(req, socket, head, (ws) => {
|
||||
wss.emit("connection", ws, req);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
wss.on("connection", (websocket, incomingMessage) => {
|
||||
logger.info(`➕ Connection (${wss.clients.size}) ${incomingMessage.method} ${incomingMessage.url}`);
|
||||
websocket.once("close", (code, reason) => {
|
||||
logger.info(`➖ Connection (${wss.clients.size}) ${code} ${reason.toString()}`);
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(port, () => {
|
||||
logger.info(`✅ Custom server ready on http://${hostname}:${port}`);
|
||||
logger.info(`✅ WebSocket Server integrated on ws://${hostname}:${port}/websockets`);
|
||||
});
|
||||
|
||||
process.on("SIGTERM", () => {
|
||||
logger.info("SIGTERM");
|
||||
wssHandler.broadcastReconnectNotification();
|
||||
wss.close();
|
||||
server.close();
|
||||
});
|
||||
});
|
||||
@@ -32,13 +32,12 @@ const getWebSocketProtocol = () => {
|
||||
};
|
||||
|
||||
const constructWebsocketUrl = () => {
|
||||
const fallback = `${getWebSocketProtocol()}://localhost:3001/websockets`;
|
||||
if (typeof window === "undefined") {
|
||||
return fallback;
|
||||
return `${getWebSocketProtocol()}://localhost:${env.NODE_ENV === "development" ? "3000" : "3001"}/websockets`;
|
||||
}
|
||||
|
||||
if (env.NODE_ENV === "development") {
|
||||
return fallback;
|
||||
return `${getWebSocketProtocol()}://localhost:3000/websockets`;
|
||||
}
|
||||
|
||||
return `${getWebSocketProtocol()}://${window.location.hostname}:${window.location.port}/websockets`;
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
"scripts": {
|
||||
"build": "esbuild src/main.ts --bundle --platform=node --outfile=wssServer.cjs --external:bcrypt --external:@opentelemetry/api --external:deasync --external:cpu-features --loader:.html=text --loader:.scss=text --loader:.node=text",
|
||||
"clean": "rm -rf .turbo node_modules",
|
||||
"dev": "pnpm with-env tsx ./src/main.ts",
|
||||
"dev": "echo 'WebSocket server is now integrated into the Next.js custom server'",
|
||||
"format": "prettier --check . --ignore-path ../../.gitignore",
|
||||
"lint": "eslint",
|
||||
"typecheck": "tsc --noEmit",
|
||||
|
||||
+2
-2
@@ -7,9 +7,9 @@ http {
|
||||
listen 7575;
|
||||
listen [::]:7575;
|
||||
|
||||
# Route websockets traffic to port 3001
|
||||
# Route websockets traffic to port 3000 (integrated custom server)
|
||||
location /websockets {
|
||||
proxy_pass http://${HOSTNAME}:3001;
|
||||
proxy_pass http://${HOSTNAME}:3000;
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Upgrade $http_upgrade;
|
||||
proxy_set_header Connection "Upgrade";
|
||||
|
||||
+1
-1
@@ -26,7 +26,7 @@
|
||||
"release": "semantic-release",
|
||||
"scripts:update-bug-report-template": "tsx ./scripts/update-bug-report-template.mts",
|
||||
"scripts:update-readme-integrations": "tsx ./scripts/update-integration-list.mts",
|
||||
"start": "concurrently \"pnpm with-env node apps/tasks/tasks.cjs\" \"pnpm with-env node apps/websocket/wssServer.cjs\" \"pnpm -F nextjs start\"",
|
||||
"start": "concurrently \"pnpm with-env node apps/tasks/tasks.cjs\" \"pnpm -F nextjs start\"",
|
||||
"test": "cross-env NODE_ENV=development CI=true vitest run --exclude e2e --coverage.enabled ",
|
||||
"test:e2e": "cross-env NODE_ENV=development CI=true vitest e2e",
|
||||
"test:ui": "cross-env NODE_ENV=development CI=true vitest --exclude e2e --ui --coverage.enabled",
|
||||
|
||||
Generated
+22
-15
@@ -325,6 +325,9 @@ importers:
|
||||
use-deep-compare-effect:
|
||||
specifier: ^1.8.1
|
||||
version: 1.8.1(react@19.2.4)
|
||||
ws:
|
||||
specifier: ^8.19.0
|
||||
version: 8.19.0
|
||||
zod:
|
||||
specifier: ^4.2.1
|
||||
version: 4.2.1
|
||||
@@ -356,18 +359,27 @@ importers:
|
||||
'@types/swagger-ui-react':
|
||||
specifier: ^5.18.0
|
||||
version: 5.18.0
|
||||
'@types/ws':
|
||||
specifier: ^8.18.1
|
||||
version: 8.18.1
|
||||
concurrently:
|
||||
specifier: ^9.2.1
|
||||
version: 9.2.1
|
||||
esbuild:
|
||||
specifier: ^0.27.3
|
||||
version: 0.27.3
|
||||
eslint:
|
||||
specifier: ^9.39.3
|
||||
version: 9.39.3
|
||||
node-loader:
|
||||
specifier: ^2.1.0
|
||||
version: 2.1.0(webpack@5.94.0)
|
||||
version: 2.1.0(webpack@5.94.0(esbuild@0.27.3))
|
||||
prettier:
|
||||
specifier: ^3.8.1
|
||||
version: 3.8.1
|
||||
tsx:
|
||||
specifier: 4.20.4
|
||||
version: 4.20.4
|
||||
typescript:
|
||||
specifier: ^5.9.3
|
||||
version: 5.9.3
|
||||
@@ -7102,9 +7114,6 @@ packages:
|
||||
get-tsconfig@4.13.6:
|
||||
resolution: {integrity: sha512-shZT/QMiSHc/YBLxxOkMtgSid5HFoauqCE3/exfsEcwg1WkeqjG+V40yBbBrsD+jW2HDXcs28xOfcbm2jI8Ddw==}
|
||||
|
||||
get-tsconfig@4.8.1:
|
||||
resolution: {integrity: sha512-k9PN+cFBmaLWtVz29SkUoqU5O0slLuHJXt/2P+tMVFT+phsSGXGkp9t3rQIqdz0e+06EHNGs3oM6ZX1s2zHxRg==}
|
||||
|
||||
git-hooks-list@4.1.1:
|
||||
resolution: {integrity: sha512-cmP497iLq54AZnv4YRAEMnEyQ1eIn4tGKbmswqwmFV4GBnAqE8NLtWxxdXa++AalfgL5EBH4IxTPyquEuGY/jA==}
|
||||
|
||||
@@ -16225,10 +16234,6 @@ snapshots:
|
||||
dependencies:
|
||||
resolve-pkg-maps: 1.0.0
|
||||
|
||||
get-tsconfig@4.8.1:
|
||||
dependencies:
|
||||
resolve-pkg-maps: 1.0.0
|
||||
|
||||
git-hooks-list@4.1.1: {}
|
||||
|
||||
git-log-parser@1.2.1:
|
||||
@@ -17645,10 +17650,10 @@ snapshots:
|
||||
dependencies:
|
||||
rrule: 2.8.1
|
||||
|
||||
node-loader@2.1.0(webpack@5.94.0):
|
||||
node-loader@2.1.0(webpack@5.94.0(esbuild@0.27.3)):
|
||||
dependencies:
|
||||
loader-utils: 2.0.4
|
||||
webpack: 5.94.0
|
||||
webpack: 5.94.0(esbuild@0.27.3)
|
||||
|
||||
node-mock-http@1.0.2: {}
|
||||
|
||||
@@ -19486,14 +19491,16 @@ snapshots:
|
||||
type-fest: 2.19.0
|
||||
unique-string: 3.0.0
|
||||
|
||||
terser-webpack-plugin@5.3.16(webpack@5.94.0):
|
||||
terser-webpack-plugin@5.3.16(esbuild@0.27.3)(webpack@5.94.0(esbuild@0.27.3)):
|
||||
dependencies:
|
||||
'@jridgewell/trace-mapping': 0.3.31
|
||||
jest-worker: 27.5.1
|
||||
schema-utils: 4.3.3
|
||||
serialize-javascript: 6.0.2
|
||||
terser: 5.46.0
|
||||
webpack: 5.94.0
|
||||
webpack: 5.94.0(esbuild@0.27.3)
|
||||
optionalDependencies:
|
||||
esbuild: 0.27.3
|
||||
|
||||
terser@5.46.0:
|
||||
dependencies:
|
||||
@@ -19686,7 +19693,7 @@ snapshots:
|
||||
tsx@4.20.4:
|
||||
dependencies:
|
||||
esbuild: 0.25.12
|
||||
get-tsconfig: 4.8.1
|
||||
get-tsconfig: 4.13.6
|
||||
optionalDependencies:
|
||||
fsevents: 2.3.3
|
||||
|
||||
@@ -20230,7 +20237,7 @@ snapshots:
|
||||
|
||||
webpack-virtual-modules@0.6.2: {}
|
||||
|
||||
webpack@5.94.0:
|
||||
webpack@5.94.0(esbuild@0.27.3):
|
||||
dependencies:
|
||||
'@types/estree': 1.0.8
|
||||
'@webassemblyjs/ast': 1.14.1
|
||||
@@ -20252,7 +20259,7 @@ snapshots:
|
||||
neo-async: 2.6.2
|
||||
schema-utils: 3.3.0
|
||||
tapable: 2.3.0
|
||||
terser-webpack-plugin: 5.3.16(webpack@5.94.0)
|
||||
terser-webpack-plugin: 5.3.16(esbuild@0.27.3)(webpack@5.94.0(esbuild@0.27.3))
|
||||
watchpack: 2.5.1
|
||||
webpack-sources: 3.3.4
|
||||
transitivePeerDependencies:
|
||||
|
||||
Executable
+114
@@ -0,0 +1,114 @@
|
||||
#!/usr/bin/env bash
|
||||
# Benchmark memory usage of the WebSocket server approach
|
||||
# Usage: ./scripts/benchmark-memory.sh [approach]
|
||||
# approach: "separate" (current: separate ws server) or "integrated" (custom Next.js server)
|
||||
|
||||
set -e
|
||||
|
||||
APPROACH="${1:-separate}"
|
||||
WAIT_SECONDS="${2:-10}"
|
||||
|
||||
echo "============================================"
|
||||
echo "Memory Benchmark: $APPROACH approach"
|
||||
echo "============================================"
|
||||
echo ""
|
||||
|
||||
measure_memory() {
|
||||
local pid=$1
|
||||
local label=$2
|
||||
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
||||
local rss
|
||||
rss=$(ps -o rss= -p "$pid" 2>/dev/null | tr -d ' ')
|
||||
if [ -n "$rss" ]; then
|
||||
local rss_mb
|
||||
rss_mb=$(echo "scale=2; $rss / 1024" | bc)
|
||||
echo "$label (PID $pid): ${rss_mb} MB (RSS: ${rss} KB)"
|
||||
echo "$rss"
|
||||
else
|
||||
echo "$label (PID $pid): unable to read memory"
|
||||
echo "0"
|
||||
fi
|
||||
else
|
||||
echo "$label: not running"
|
||||
echo "0"
|
||||
fi
|
||||
}
|
||||
|
||||
cleanup() {
|
||||
echo ""
|
||||
echo "Cleaning up processes..."
|
||||
for pid in "${PIDS[@]}"; do
|
||||
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
|
||||
kill "$pid" 2>/dev/null || true
|
||||
fi
|
||||
done
|
||||
wait 2>/dev/null || true
|
||||
echo "Done."
|
||||
}
|
||||
|
||||
trap cleanup EXIT
|
||||
|
||||
PIDS=()
|
||||
TOTAL_RSS=0
|
||||
|
||||
if [ "$APPROACH" = "separate" ]; then
|
||||
echo "Starting separate WebSocket server (current approach)..."
|
||||
echo "---"
|
||||
|
||||
# Start the WebSocket server
|
||||
node apps/websocket/wssServer.cjs &
|
||||
WSS_PID=$!
|
||||
PIDS+=("$WSS_PID")
|
||||
|
||||
# Start the Next.js server
|
||||
node apps/nextjs/server.js &
|
||||
NEXTJS_PID=$!
|
||||
PIDS+=("$NEXTJS_PID")
|
||||
|
||||
echo "Waiting ${WAIT_SECONDS}s for processes to stabilize..."
|
||||
sleep "$WAIT_SECONDS"
|
||||
|
||||
echo ""
|
||||
echo "--- Memory Usage (Separate Approach) ---"
|
||||
wss_mem=$(measure_memory "$WSS_PID" "WebSocket Server" | tail -1)
|
||||
nextjs_mem=$(measure_memory "$NEXTJS_PID" "Next.js Server" | tail -1)
|
||||
|
||||
TOTAL_RSS=$((wss_mem + nextjs_mem))
|
||||
TOTAL_MB=$(echo "scale=2; $TOTAL_RSS / 1024" | bc)
|
||||
|
||||
echo ""
|
||||
echo "TOTAL: ${TOTAL_MB} MB (RSS: ${TOTAL_RSS} KB)"
|
||||
|
||||
elif [ "$APPROACH" = "integrated" ]; then
|
||||
echo "Starting integrated custom Next.js server (new approach)..."
|
||||
echo "---"
|
||||
|
||||
# Start the custom server (Next.js + WebSocket integrated)
|
||||
node apps/nextjs/customServer.cjs &
|
||||
CUSTOM_PID=$!
|
||||
PIDS+=("$CUSTOM_PID")
|
||||
|
||||
echo "Waiting ${WAIT_SECONDS}s for process to stabilize..."
|
||||
sleep "$WAIT_SECONDS"
|
||||
|
||||
echo ""
|
||||
echo "--- Memory Usage (Integrated Approach) ---"
|
||||
custom_mem=$(measure_memory "$CUSTOM_PID" "Custom Server (Next.js + WS)" | tail -1)
|
||||
|
||||
TOTAL_RSS=$custom_mem
|
||||
TOTAL_MB=$(echo "scale=2; $TOTAL_RSS / 1024" | bc)
|
||||
|
||||
echo ""
|
||||
echo "TOTAL: ${TOTAL_MB} MB (RSS: ${TOTAL_RSS} KB)"
|
||||
|
||||
else
|
||||
echo "Unknown approach: $APPROACH"
|
||||
echo "Usage: $0 [separate|integrated] [wait_seconds]"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo ""
|
||||
echo "============================================"
|
||||
echo "Benchmark complete for: $APPROACH"
|
||||
echo "Total memory: ${TOTAL_MB} MB"
|
||||
echo "============================================"
|
||||
+3
-5
@@ -48,16 +48,14 @@ fi
|
||||
node apps/tasks/tasks.cjs &
|
||||
TASKS_PID=$!
|
||||
|
||||
node apps/websocket/wssServer.cjs &
|
||||
WSS_PID=$!
|
||||
|
||||
node apps/nextjs/server.js &
|
||||
# WebSocket server is now integrated into the custom Next.js server
|
||||
node apps/nextjs/customServer.cjs &
|
||||
NEXTJS_PID=$!
|
||||
|
||||
# Function to handle SIGTERM and shut down services
|
||||
terminate() {
|
||||
echo "Received SIGTERM. Shutting down..."
|
||||
kill -TERM $NGINX_PID $TASKS_PID $WSS_PID $NEXTJS_PID 2>/dev/null
|
||||
kill -TERM $NGINX_PID $TASKS_PID $NEXTJS_PID 2>/dev/null
|
||||
wait
|
||||
# kill redis-server last because of logging of other services and only if $REDIS_PID is set
|
||||
if [ -n "$REDIS_PID" ]; then
|
||||
|
||||
Reference in new issue
Block a user