feat/fix: implement WebSockets for NowPlaying, better data fetching with addl. Last.fm fetch, docker build fix

This commit is contained in:
Aidan 2025-09-07 00:09:06 -04:00
parent 4cec7406c3
commit 7121ec926f
11 changed files with 514 additions and 175 deletions

45
server.ts Normal file
View file

@ -0,0 +1,45 @@
import { createServer } from "node:http";
import next from "next";
import { Server } from "socket.io";
import { NowPlayingService } from "./lib/now-playing-server";
const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = parseInt(process.env.PORT || "3000", 10);
const app = next({ dev, hostname, port });
const handler = app.getRequestHandler();
app.prepare().then(() => {
const httpServer = createServer(handler);
const io = new Server(httpServer);
const nowPlayingService = new NowPlayingService(io);
io.on("connection", (socket) => {
console.log("[WS] Client connected:", socket.id);
socket.on("requestNowPlaying", async () => {
await nowPlayingService.fetchNowPlaying(socket.id);
});
socket.on("startAutoRefresh", () => {
const intervalId = setInterval(async () => {
await nowPlayingService.fetchNowPlaying(socket.id);
}, 30000);
socket.on("disconnect", () => {
clearInterval(intervalId);
console.log("[WS] Client disconnected:", socket.id);
});
});
socket.on("disconnect", () => {
console.log("[WS] Client disconnected:", socket.id);
});
});
httpServer.listen(port, () => {
console.log(`✓ Ready on http://${hostname}:${port}`);
});
});