Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion for implementation of clients API #1966

Merged
merged 1 commit into from Jan 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Readme.md
Expand Up @@ -240,6 +240,40 @@ server.listen(3000);
Hash of `Socket` objects that are connected to this namespace indexed
by `id`.

### Namespace#clients(fn:Function)

Gets a list of client IDs connected to this namespace (across all nodes if applicable).

An example to get all clients in a namespace:

```js
var io = require('socket.io')();
io.of('/chat').clients(function(error, clients){
if (error) throw error;
console.log(clients); // => [PZDoMHjiu8PYfRiKAAAF, Anw2LatarvGVVXEIAAAD]
});
```

An example to get all clients in namespace's room:

```js
var io = require('socket.io')();
io.of('/chat').in('general').clients(function(error, clients){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also add an example of getting all rooms

if (error) throw error;
console.log(clients); // => [Anw2LatarvGVVXEIAAAD]
});
```

As with broadcasting, the default is all clients from the default namespace ('/'):

```js
var io = require('socket.io')();
io.clients(function(error, clients){
if (error) throw error;
console.log(clients); // => [6em3d4TJP8Et9EMNAAAA, G5p55dHhGgUnLUctAAAB]
});
```

### Namespace#use(fn:Function):Namespace

Registers a middleware, which is a function that gets executed for
Expand Down
2 changes: 1 addition & 1 deletion lib/index.js
Expand Up @@ -361,7 +361,7 @@ Server.prototype.close = function(){
* Expose main namespace (/).
*/

['on', 'to', 'in', 'use', 'emit', 'send', 'write'].forEach(function(fn){
['on', 'to', 'in', 'use', 'emit', 'send', 'write', 'clients'].forEach(function(fn){
Server.prototype[fn] = function(){
var nsp = this.sockets[fn];
return nsp.apply(this.sockets, arguments);
Expand Down
12 changes: 12 additions & 0 deletions lib/namespace.js
Expand Up @@ -240,3 +240,15 @@ Namespace.prototype.write = function(){
this.emit.apply(this, args);
return this;
};

/**
* Gets a list of clients.
*
* @return {Namespace} self
* @api public
*/

Namespace.prototype.clients = function(fn){
this.adapter.clients(this.rooms, fn);
return this;
};
116 changes: 116 additions & 0 deletions test/socket.io.js
Expand Up @@ -614,6 +614,122 @@ describe('socket.io', function(){
});
});
});

it('should find all clients in a namespace', function(done){
var srv = http();
var sio = io(srv);
var chatSids = [];
var otherSid = null;
srv.listen(function(){
var c1 = client(srv, '/chat');
var c2 = client(srv, '/chat', {forceNew: true});
var c3 = client(srv, '/other', {forceNew: true});
var total = 3;
sio.of('/chat').on('connection', function(socket){
chatSids.push(socket.id);
--total || getClients();
});
sio.of('/other').on('connection', function(socket){
otherSid = socket.id;
--total || getClients();
});
});
function getClients() {
sio.of('/chat').clients(function(error, sids) {
expect(error).to.be.undefined;
expect(sids).to.contain(chatSids[0]);
expect(sids).to.contain(chatSids[1]);
expect(sids).to.not.contain(otherSid);
done();
});
}
});

it('should find all clients in a namespace room', function(done){
var srv = http();
var sio = io(srv);
var chatFooSid = null;
var chatBarSid = null;
var otherSid = null;
srv.listen(function(){
var c1 = client(srv, '/chat');
var c2 = client(srv, '/chat', {forceNew: true});
var c3 = client(srv, '/other', {forceNew: true});
var chatIndex = 0;
var total = 3;
sio.of('/chat').on('connection', function(socket){
if (chatIndex++) {
socket.join('foo', function() {
chatFooSid = socket.id;
--total || getClients();
});
} else {
socket.join('bar', function() {
chatBarSid = socket.id;
--total || getClients();
});
}
});
sio.of('/other').on('connection', function(socket){
socket.join('foo', function() {
otherSid = socket.id;
--total || getClients();
});
});
});
function getClients() {
sio.of('/chat').in('foo').clients(function(error, sids) {
expect(error).to.be.undefined;
expect(sids).to.contain(chatFooSid);
expect(sids).to.not.contain(chatBarSid);
expect(sids).to.not.contain(otherSid);
done();
});
}
});

it('should find all clients across namespace rooms', function(done){
var srv = http();
var sio = io(srv);
var chatFooSid = null;
var chatBarSid = null;
var otherSid = null;
srv.listen(function(){
var c1 = client(srv, '/chat');
var c2 = client(srv, '/chat', {forceNew: true});
var c3 = client(srv, '/other', {forceNew: true});
var chatIndex = 0;
var total = 3;
sio.of('/chat').on('connection', function(socket){
if (chatIndex++) {
socket.join('foo', function() {
chatFooSid = socket.id;
--total || getClients();
});
} else {
socket.join('bar', function() {
chatBarSid = socket.id;
--total || getClients();
});
}
});
sio.of('/other').on('connection', function(socket){
socket.join('foo', function() {
otherSid = socket.id;
--total || getClients();
});
});
});
function getClients() {
sio.of('/chat').clients(function(error, sids) {
expect(error).to.be.undefined;
expect(sids).to.contain(chatFooSid);
expect(sids).to.contain(chatBarSid);
expect(sids).to.not.contain(otherSid);
done();
});
}
});
});

describe('socket', function(){
Expand Down