Advertisement
EdmundC

server

Sep 28th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const http = require('http');
  3. const socketIo = require('socket.io');
  4.  
  5. const app = express();
  6. const server = http.createServer(app);
  7. const io = socketIo(server);
  8.  
  9. app.use(express.static('public')); // Serve static files from the public directory
  10.  
  11. io.on('connection', (socket) => {
  12.     console.log('A user connected');
  13.  
  14.     socket.on('chat message', (msg) => {
  15.         io.emit('chat message', msg); // Broadcast the message to all clients
  16.     });
  17.  
  18.     socket.on('disconnect', () => {
  19.         console.log('A user disconnected');
  20.     });
  21. });
  22.  
  23. const PORT = process.env.PORT || 3000;
  24. server.listen(PORT, () => {
  25.     console.log(`Server is running on http://localhost:${PORT}`);
  26. });
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement