Register Chat Events

Import the chat event registration function into src/server.js.

After initializing Socket.IO, register the event handlers using the io instance.

Updated src/server.js
import 'dotenv/config';
import express from 'express';
import { createServer } from 'node:http';
import { initializeSocket } from './config/socket.js';
import { registerChatEvents } from './events/chat.events.js';

const app = express();

app.use(express.json());
app.use(express.static('public'));

app.get('/health', (req, res) => {
  res.json({ status: 'ok' });
});

const httpServer = createServer(app);
const io = initializeSocket(httpServer);

registerChatEvents(io);

httpServer.listen(process.env.PORT || 3000, () => {
  console.log('Chat application running');
});

Initializes the server and registers all chat-related Socket.IO events before accepting connections.

Key Takeaways
  • The event module is registered using the Socket.IO instance.

  • The server file remains responsible for application startup.

  • Event registration should happen before accepting client connections.