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:
copilot-swe-agent[bot]andajnart committed 2026-02-28 08:58:34 +00:00
1 parent 000604bd02
commit c8ae7f283a
11 files changed
+248 -31

No files matched your search

+114
View File
@@ -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
View File
@@ -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