Broadcast a message to all other connected sockets except for itself. Used most often after a socket level event occurs. E.g., a new message has arrived from an existing client socket, and you want to relay it onto all the other clients.
import{createServer}from'http'import{Server}from'socket.io'import*asexpressfrom'express'import*aspathfrom'path'constport=3000constapp=express()app.use(express.static(path.join(__dirname,'../client')))constserver=createServer(app)constio=newServer(server)io.on('connection',(socket)=>{console.log('a user connected : '+socket.id)socket.emit('message','Hello '+socket.id)socket.broadcast.emit('message','Everybody, say hello to '+socket.id)socket.on('disconnect',()=>{console.log('socket disconnected : '+socket.id)socket.broadcast.emit('message',socket.id+' has left the building')})})server.listen(port,()=>{console.log('Server listening on port '+port)})