const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static("public"));
io.on("connection", (socket) => {
console.log("User connected");
socket.on("chat message", (msg) => {
io.emit("chat message", msg);
});
socket.on("disconnect", () => {
console.log("User disconnected");
});
});
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
<title>Group Chat</title>
<style>
body { font-family: Arial; background: #f4f4f4; }
#messages { list-style: none; padding: 0; }
#messages li { padding: 8px; background: white; margin: 5px 0; border-radius: 5px; }
input { padding: 10px; width: 80%; }
button { padding: 10px; }
</style>
Group Chat
Send
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
const input = document.getElementById("input");
const messages = document.getElementById("messages");
function sendMessage() {
if (input.value) {
socket.emit("chat message", input.value);
input.value = "";
}
}
socket.on("chat message", function(msg) {
const li = document.createElement("li");
li.textContent = msg;
messages.appendChild(li);
});
</script>
const express = require("express");
const http = require("http");
const { Server } = require("socket.io");
const app = express();
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static("public"));
io.on("connection", (socket) => {
console.log("User connected");
});
server.listen(3000, () => {
<title>Group Chat</title> <style> body { font-family: Arial; background: #f4f4f4; } #messages { list-style: none; padding: 0; } #messages li { padding: 8px; background: white; margin: 5px 0; border-radius: 5px; } input { padding: 10px; width: 80%; } button { padding: 10px; } </style>console.log("Server running on http://localhost:3000");
});
Group Chat
Send