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');
Broadcasting also works with multiple Socket.IO servers. You just need to replace the default Adapter by the Redis Adapter.
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.
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.
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.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With