Server has been deployed to https://server-chat-js.herokuapp.com/
Tạo SocketIO Server thông qua việc sử dụng thư viện Socket.io, Express và một HTTP Server
Cài đặt các package cần thiết
npm i express socket.io uuid
Cài đặt Sever
const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
io.on("connection", (socket) => {
// Some events and communication
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
// Listen when user join to room
socket.on("join", ({ name }) => {
// Something ...
});
// Listen when user send message to room
socket.on("send_message", ({ id, message }) => {
// Something ...
});
// Listen when user disconnect to Socket.IO Server
socket.on("disconnect", () => {
// Something ...
});
const listener = (...args) => {
console.log(args);
}
socket.on("event", listener);
// Remove Event "event"
socket.off("event", listener);
// Remove all events
socket.removeAllListeners();
// server-side
io.on("connection", (socket) => {
socket.on("message", (mess) => {
console.log(mess); // print message
});
});
// client-side
socket.emit("message", {mess: "Hello world"});
// Send message to all members except sender
io.on("connection", (socket) => {
socket.broadcast.emit("message", "Send to all member except sender");
});