Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to broadcast when out of socket.io loop

I have following code on server side, which does function call on new connection with socket.

var io = require('socket.io').listen(80);

io.sockets.on('connection', function (socket) {
  socket.broadcast.emit('user connected');
});

now i have another module that takes rest request and it want to broadcast on all open sockets. How can it be done?

Is there any way to use broadcast without socket? like

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

Edit more like this?

io.sockets.broadcast.to('m1').emit('user message', 'from', 'msg');
like image 487
mamu Avatar asked Jul 27 '11 18:07

mamu


People also ask

Is broadcasting possible in socket?

Broadcasting also works with multiple Socket.IO servers. You just need to replace the default Adapter by the Redis Adapter.

Does Socket.IO use long polling?

js) and the Socket.IO client (browser, Node. js, or another programming language) is established with a WebSocket connection whenever possible, and will use HTTP long-polling as fallback.

Is Socket.IO emit asynchronous?

JS, Socket.IO enables asynchronous, two-way communication between the server and the client. This means that the server can send messages to the client without the client having to ask first, as is the case with AJAX.

How many players can Socket.IO handle?

Socket.io maxes out my CPU at around 3000 concurrent users. This is on Intel i7 CPU. Because of this I have to run multiple node/socket.io processes to handle the load. For 100 concurrent connections you should be fine.


1 Answers

Yes, indeed! Just call it on io.sockets.

io.sockets.emit('user connected')

This is pretty much exactly what broadcast does, except that broadcast takes the extra step of excluding itself from the broadcast.

Edit:

To scope to the room, like you've specified, do this:

io.sockets.in('m1').emit('user message', 'from', 'msg');
like image 95
Jeremy Roman Avatar answered Oct 31 '22 19:10

Jeremy Roman