import { Server } from 'socket.io' export default (_, nuxt) => { nuxt.hook('listen', server => { const io = new Server(server) nuxt.hook('close', () => io.close()) io.on('connection', (socket) => { console.log('Connection', socket.id) }) io.on('connect', (socket) => { socket.emit('message', `welcome ${socket.id}`) socket.broadcast.emit('message', `${socket.id} joined`) socket.on('joinRoom', (room) => { socket.join(room) socket.emit('message', `joinRoom ${room}`) socket.broadcast.to(room).emit('message', `${socket.id} joined ${room}`) }) socket.on('newComment', function comment(message, room) { console.log('new comment received: %s', message) socket.broadcast.to(room).emit('newComment', { message }) }) socket.on('message', function message(data) { console.log('message received: %s', data) socket.broadcast.emit('message', { data }) }) socket.on('disconnecting', () => { console.log('disconnected', socket.id) socket.broadcast.emit('message', `${socket.id} left`) }) }) }) }