javascript - Broadcast message to all clients -
i've tried broadcasting message clients no success.
the marked line works 1 client.
i've tried these options: socket.emit socket.broadcast.emit
thanks
you calling on
function on wrong object. should rename socket
variable else. commonly, people use io
.
io = require('socket.io').listen(server);
then, need call emit
on sockets
send users:
io.sockets.on('connection', function(socket) { io.sockets.emit('message', data); });
of course, send
emit
uses message
event. can use instead inside callback:
io.sockets.send(data);
in either case, have following in client react event:
socket.on('message', function (data) { // things data });
note: socket.broadcast.emit
can used send event users connected other user connected socket
.
Comments
Post a Comment