Install socket.io
npm install socket.io socket.io-client
Server
import { Server } from "socket.io";
...
// socket.io
const io = new Server(server, { // create websocket endpoint so that server & client can talk to each other
cors: {
origin: "*",
methods: ['GET', "POST"]
}
});
io.on("connection", (socket) => {
console.log(`user ${socket.id} is connected.`);
socket.on('disconnect', () => {
console.log(`user ${socket.id} left.`);
});
});
Client
import io from "socket.io-client";
...
created() {
// initialize socket
this.socketInstance = io("http://localhost:3000");
}