Skip to content
This repository has been archived by the owner on Sep 28, 2021. It is now read-only.

Print passport data of connected sockets in a room #122

Open
marcogreselin opened this issue Aug 3, 2016 · 6 comments
Open

Print passport data of connected sockets in a room #122

marcogreselin opened this issue Aug 3, 2016 · 6 comments

Comments

@marcogreselin
Copy link

I'm using passport.socketio to get user credentials and I want to print to the console the list of connected users in a given room every time a user connect. Currently I have the following:

io.use(passportSocketIo.authorize({
    key: 'connect.sid',
    secret: 'yuppy',
    store: sessionStore,
    cookieParser: cookieParser,
}));

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on("logon", function () {
        socket.join(socket.request.user.companyid);
        console.log(io.nsps['/'].adapter.rooms[socket.request.user.companyid])
    })
});

But it prints something like:

Room { sockets: { '/#PteHXocxKH8Ctv6TAAAB': true }, length: 1 }

How can I print the Passport data?

@jfromaniello
Copy link
Owner

can you try something like this:

var currentRoom = io.nsps['/'].adapter.rooms[socket.request.user.companyid];
var currentUsersInRoom = currentRoom.sockets.map(s => s.request.user);
console.dir(currentUsersInRoom);

I haven't tested this, the reason I have to believe this should work is because of this doc Socket#request which states:

A getter proxy that returns the reference to the request that
originated the underlying engine.io Client. Useful for accessing
request headers such as Cookie or User-Agent.

console.log(room) will not show this because is not a enumerable property maybe.

Let me know if if works

@marcogreselin
Copy link
Author

I get:

Missing error handler on `socket`.
TypeError: Cannot read property 'sockets' of undefined

@marcogreselin
Copy link
Author

Sorry this is the error:

TypeError: currentRoom.sockets.map is not a function

@jfromaniello
Copy link
Owner

Yes, is a little bit more complicated than what I though. Here is an answer on SO:

http://stackoverflow.com/questions/6563885/socket-io-how-do-i-get-a-list-of-connected-sockets-clients

Maybe something like this:

function findClientsSocket(roomId, namespace) {
    var res = []
    , ns = io.of(namespace ||"/");    // the default namespace is "/"

    if (ns) {
        for (var id in ns.connected) {
            if(roomId) {
                var index = ns.connected[id].rooms.indexOf(roomId) ;
                if(index !== -1) {
                    res.push(ns.connected[id]);
                }
            } else {
                res.push(ns.connected[id]);
            }
        }
    }
    return res;
}
//then

var users = findClientsSocket(socket.request.user.companyid).map(s => s.request.user);

console.dir(users, { depth: 10 });

@marcogreselin
Copy link
Author

This is what I get in ns.connect. Does not look like an array to me. It does not look like an object either. I don't know.

8:47:25 PM web.1 |  { '/#AKNsIaR5Mu89frJBAAAB': 
8:47:25 PM web.1 |     Socket {
8:47:25 PM web.1 |       nsp: 
8:47:25 PM web.1 |        Namespace {
8:47:25 PM web.1 |          name: '/',
8:47:25 PM web.1 |          server: [Object],
8:47:25 PM web.1 |          sockets: [Object],
8:47:25 PM web.1 |          connected: [Circular],
8:47:25 PM web.1 |          fns: [],
8:47:25 PM web.1 |          ids: 0,
8:47:25 PM web.1 |          adapter: [Object],
8:47:25 PM web.1 |          _events: [Object],
8:47:25 PM web.1 |          _eventsCount: 1 },
8:47:25 PM web.1 |       server: 
8:47:25 PM web.1 |        Server {
8:47:25 PM web.1 |          nsps: [Object],
8:47:25 PM web.1 |          _path: '/socket.io',
8:47:25 PM web.1 |          _serveClient: true,
8:47:25 PM web.1 |          _adapter: [Function: Adapter],
8:47:25 PM web.1 |          _origins: '*:*',
8:47:25 PM web.1 |          sockets: [Object],
8:47:25 PM web.1 |          eio: [Object],
8:47:25 PM web.1 |          httpServer: [Object],
8:47:25 PM web.1 |          engine: [Object] },
8:47:25 PM web.1 |       adapter: 
8:47:25 PM web.1 |        Adapter {
8:47:25 PM web.1 |          nsp: [Object],
8:47:25 PM web.1 |          rooms: [Object],
8:47:25 PM web.1 |          sids: [Object],
8:47:25 PM web.1 |          encoder: Encoder {} },
8:47:25 PM web.1 |       id: '/#AKNsIaR5Mu89frJBAAAB',
8:47:25 PM web.1 |       client: 
8:47:25 PM web.1 |        Client {
8:47:25 PM web.1 |          server: [Object],
8:47:25 PM web.1 |          conn: [Object],
8:47:25 PM web.1 |          encoder: Encoder {},
8:47:25 PM web.1 |          decoder: [Object],
8:47:25 PM web.1 |          id: 'AKNsIaR5Mu89frJBAAAB',
8:47:25 PM web.1 |          request: [Object],
8:47:25 PM web.1 |          onclose: [Function: bound ],
8:47:25 PM web.1 |          ondata: [Function: bound ],
8:47:25 PM web.1 |          onerror: [Function: bound ],
8:47:25 PM web.1 |          ondecoded: [Function: bound ],
8:47:25 PM web.1 |          sockets: [Object],
8:47:25 PM web.1 |          nsps: [Object],
8:47:25 PM web.1 |          connectBuffer: [] },
8:47:25 PM web.1 |       conn: 
8:47:25 PM web.1 |        Socket {
8:47:25 PM web.1 |          id: 'AKNsIaR5Mu89frJBAAAB',
8:47:25 PM web.1 |          server: [Object],
8:47:25 PM web.1 |          upgrading: true,
8:47:25 PM web.1 |          upgraded: false,
8:47:25 PM web.1 |          readyState: 'open',
8:47:25 PM web.1 |          writeBuffer: [],
8:47:25 PM web.1 |          packetsFn: [],
8:47:25 PM web.1 |          sentCallbackFn: [],
8:47:25 PM web.1 |          cleanupFn: [Object],
8:47:25 PM web.1 |          request: [Object],
8:47:25 PM web.1 |          remoteAddress: '::1',
8:47:25 PM web.1 |          checkIntervalTimer: [Object],
8:47:25 PM web.1 |          upgradeTimeoutTimer: [Object],
8:47:25 PM web.1 |          pingTimeoutTimer: [Object],
8:47:25 PM web.1 |          transport: [Object],
8:47:25 PM web.1 |          _events: [Object],
8:47:25 PM web.1 |          _eventsCount: 3 },
8:47:25 PM web.1 |       rooms: { '/#AKNsIaR5Mu89frJBAAAB': '/#AKNsIaR5Mu89frJBAAAB' },
8:47:25 PM web.1 |       acks: {},
8:47:25 PM web.1 |       connected: true,
8:47:25 PM web.1 |       disconnected: false,
8:47:25 PM web.1 |       handshake: 
8:47:25 PM web.1 |        { headers: [Object],
8:47:25 PM web.1 |          time: 'Wed Aug 03 2016 20:47:25 GMT+0100 (BST)',
8:47:25 PM web.1 |          address: '::1',
8:47:25 PM web.1 |          xdomain: false,
8:47:25 PM web.1 |          secure: false,
8:47:25 PM web.1 |          issued: 1470253645235,
8:47:25 PM web.1 |          url: '/socket.io/?EIO=3&transport=polling&t=LPHyZsk',
8:47:25 PM web.1 |          query: [Object] },
8:47:25 PM web.1 |       _events: { incomingCall: [Function], logon: [Function] },
8:47:25 PM web.1 |       _eventsCount: 2 } }

@jfromaniello
Copy link
Owner

is an object and the expression for (var id in ns.connected) { will iterate over the keys, the id will be something like this '/#AKNsIaR5Mu89frJBAAAB'

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants