node.js - Socket emit to room works for all sockets except the sender -


i'm creating voting feature using socket.io. sockets placed rooms channelid. whenever socket in room emits vote event, i'm trying emit current vote-list sockets in room.

my problem if have sockets a, b, c in same room, votes socket visible b , c (that is, b , c's on.('vote') listeners called), not itself.

what expect happen, if sockets a, b, c in same room, , emits vote, sockets a, b, c vote listeners called.

client:

these listeners defined within methods, i've singled them out clarity. variables defined.

const socket = io('https://localhost:3001')  socket.emit('join-channel',{      channelid: payload.channelid,     senderid: socket.id  })  socket.emit('vote',{     senderid: socket.id,     channelid: state.channelid,     vote: payload.vote,     userid: state.userid })  socket.on(`vote`, function (data) {     store.commit(mutations.set_votes, data) }); 

server

module.exports = (app,server) => { var io = require('socket.io')(server);

io.on('connection', function (socket) {      socket.on('join-channel',data=>{         socket.join(data.channelid)     })      socket.on('vote',data=>{         postvote(data)         let { channelid } = data          socket.to(channelid).emit(`vote`,store[channelid])         //socket.emit(`vote`,store[channelid]) //my workaround socket's messages emitted     })   }); 

};

my current workaround add socket.emit('vote',store[channelid]), can emit data itself.

this design in socket.io api. form using:

socket.to(channelid).emit(...) 

is designed send sockets in channelid room except socket.

if want send users in room, change above code to:

io.to(channelid).emit(...) 

here's quote socket.io doc socket.to():

sets modifier subsequent event emission event broadcasted clients have joined given room (the socket being excluded).


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -