Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const express = require('express');
- const http = require('http');
- const socketIo = require('socket.io');
- const app = express();
- const server = http.createServer(app);
- const io = socketIo(server);
- app.use(express.static('public')); // Serve static files from the public directory
- io.on('connection', (socket) => {
- console.log('A user connected');
- socket.on('chat message', (msg) => {
- io.emit('chat message', msg); // Broadcast the message to all clients
- });
- socket.on('disconnect', () => {
- console.log('A user disconnected');
- });
- });
- const PORT = process.env.PORT || 3000;
- server.listen(PORT, () => {
- console.log(`Server is running on http://localhost:${PORT}`);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement