How to broadcast when from socket.io loop

I have the following server-side code that performs the function of invoking a new socket connection.

var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.broadcast.emit('user connected'); }); 

now I have another module that accepts a vacation request and wants to broadcast all open sockets. How can I do that?

Can I use broadcast without a socket? as

 io.broadcast.emit('user connected'); 

Edit is more like this?

 io.sockets.broadcast.to('m1').emit('user message', 'from', 'msg'); 
+6
source share
1 answer

Yes indeed! Just name it at io.sockets.

 io.sockets.emit('user connected') 

This is exactly what broadcast does, except that broadcast takes an extra step that excludes itself from the broadcast.

Edit:

In scope, as you indicated, follow these steps:

 io.sockets.in('m1').emit('user message', 'from', 'msg'); 
+7
source

Source: https://habr.com/ru/post/893710/


All Articles