Global Broadcast
I expect that if I call io.sockets.emit, all connected clients regardless of namespace will receive the specified event. In the following example, this does not happen, and I don't understand why.
Server:
var express = require('express');
var http = require('http');
var fs = require('fs');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
io.of('/public').on('connection', function(socket) {
socket.on('test', function(value) {
io.sockets.emit('super', value);
io.of('/public').emit('sub', value);
});
});
app.get('/', function(req, res) {
res.sendfile(__dirname + '/client.html');
});
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('listening on', port);
});
Client:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Socket Test</title>
</head>
<body>
<div id="input">null</div>
<div id="super">null</div>
<div id="sub">null</div>
<script src="/socket.io/socket.io.js"></script>
<script>
var $ = document.querySelector.bind(document);
var socket = io.connect('/public', {
'sync disconnect on unload': true,
'flash policy port': -1
});
var id = ((Math.random() * 1000) | 0) + '';
$("#input").textContent = id;
socket.emit('test', id);
socket.on('super', function(value) {
console.log('super: expected', value, 'to equal', id);
$("#super").textContent = value;
});
socket.on('sub', function(value) {
console.log('sub: expected', value, 'to equal', id);
$("#sub").textContent = value;
});
</script>
</body>
</html>
Output:
358
null
358
Console:
sub: expected 546 to equal 546
Server Console:
node server
info - socket.io started
listening on 3030
debug - client authorized
info - handshake authorized uWg_ZUcjOwLwwHYS3zXs
debug - setting request GET /socket.io/1/websocket/uWg_ZUcjOwLwwHYS3zXs
debug - set heartbeat interval for client uWg_ZUcjOwLwwHYS3zXs
debug - client authorized for
debug - websocket writing 1::
debug - client authorized for /public
debug - websocket writing 1::/public
debug - websocket writing 5:::{"name":"super","args":["285"]}
debug - websocket writing 5::/public:{"name":"sub","args":["285"]}
info - transport end (socket end)
debug - set close timeout for client uWg_ZUcjOwLwwHYS3zXs
debug - cleared close timeout for client uWg_ZUcjOwLwwHYS3zXs
debug - cleared heartbeat interval for client uWg_ZUcjOwLwwHYS3zXs
debug - discarding transport
@skeggse Hey, from my understanding io.emit() only broadcasts to the default namespace of '/'. '/public' is not a part of the '/' namespace. I think the super / sub idea might be problematic -- I believe the namespaces exist to keep the sockets theoretically separate. Thus they don't really share a common '/' parent.
A solution to broadcasting to all clients regardless of namespace might be something like this:
var namespaces = ['/public', '/private'];
function superBroadcast(event, data) {
for (var i = 0; i < namespaces.length; i++) {
var ns = namespaces[i];
io.of(ns).emit(event, data);
}
}
That's more or less what I figured out. That doesn't make a whole lot of sense to me, though, given that io.sockets is supposed to represent the set of all sockets connected to the server.
The documentation seems to be a little contradictory, if not indicating the opposite. The following states that the emit method sends the event to all connected clients, not to all clients on the default namespace.
Server#emit
Emits an event to all connected clients. The following two are equivalent:
var io = require('socket.io')(); io.sockets.emit('an event sent to all connected clients'); io.emit('an event sent to all connected clients');
Seems like the docs need to be clarified, I suppose. That said, having the ability to broadcast to all namespaces would be a nice feature, especially if you have a number of namespaces like /public and /private that should all be notified of certain state changes.
@skeggse that's a good point about the docs, definitely unclear.
@skeggse and potentially a good feature yeah