Dries Cleymans
Dries Cleymans

Reputation: 890

Primus + primus-rooms + sockjs: How to retrieve all clients connected to a room

I have a primus server that runs on the sockjs transformer. I'm using the primus-rooms plugin to support rooms. Joining and leaving rooms is successful, I can send messages to clients that are connected to specific rooms.

At some point I need to retrieve all the connected clients in a specific room. I want to use this: https://github.com/cayasso/primus-rooms#primusroomroomclientsfn

primus.room('room').clients(fn);

But the fn function always return null, even if there are clients connected to the room I'm targeting. This is my implementation:

var remoteRoom = 'the_room_I_need_to_target';
primus.room(remoteRoom).clients(function(clients){
    console.dir('primus clients in room ' + remoteRoom + ': ' + clients);
});

Do I need to configure something extra to get this to run? I'm thinking I need to configure an adapter for the rooms but I'm unsure what that does and how it should be done.


To be somewhat complete, this is my initial setup of Primus:

var app = require('express')();
var server = require('http').Server(app);
var Primus = require('primus');
var Rooms = require('primus-rooms');
var primus = new Primus(server, {transformer: 'sockjs', pathname: '/primus/my-room'});
primus.plugin('rooms', Rooms);

I can join and leave rooms successfully. Joining & leaving is triggered by some actions in the client:

primus.on('connection', function (spark) {
    spark.on('data', function(data) {

    data = data || {};

    var action = data.action;
    var roomToJoin = data.roomToJoin;

    if ('join' === action) { //join room
        spark.join(roomToJoin, function () {
            logger.debug('primus id ' + spark.id + ' joined room ' + roomToJoin);    
        });
    }else if ('leave' === action) { //leave room
        spark.leave(roomToJoin, function () {
            logger.debug('primus id ' + spark.id + ' left room ' + roomToJoin);

        });
    }
}

Upvotes: 1

Views: 282

Answers (1)

Dries Cleymans
Dries Cleymans

Reputation: 890

The handler function is fn(error, clients) and not fn(clients). Changing the code to the following resulted in the expected behavior:

var remoteRoom = 'the_room_I_need_to_target';
primus.room(remoteRoom).clients(function(error, clients){
    console.dir('primus clients in room ' + remoteRoom + ': ' + clients);
});

Upvotes: 2

Related Questions