const express = require('express');
const { WebSocketServer } = require('ws');
const path = require('path');
const { subscribeToSomething, setWsClient } = require('./subscriptionClient');

const app = express();
const PORT = 4000;

// Serve the HTML file
app.get('/', (req, res) => {
  res.sendFile(path.join(__dirname, 'index.html'));
});

const server = app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

// Create WebSocket server
const wss = new WebSocketServer({ server });

wss.on('connection', (ws) => {
  console.log('Client connected');
  setWsClient(ws);

  ws.on('close', () => {
    console.log('Client disconnected');
  });
});

// Start the subscription
subscribeToSomething();
