How to get all currently connected users (websocket extension)


#1

What I am looking to do is get the names of all currently connected clients. I have the code below:

var chanId  = arguments.websocket.getPathParameters().channel;
var connMgr = arguments.websocket.getConnectionManager();

var currentSubscribers = ArrayMap(connMgr.getChannel(chanId).getSubscribers().toArray(), function(subscriber) {
    return arguments.subscriber.getSessionScope().user.label;
});

This seems to do what I want because inside the sessionScope is a user struct that contains the name of the user. Is there a better way to accomplish the same thing?

Another thing I was wondering about is that if I put the above code in the (onClose, onUnsubscribe) functions and I have two users connected to a channel and I navigate one user away it will still show the user that navigated away in the array returned. Maybe I am doing something wrong. Thanks in advance for any help and creating such a great extension!


#2

It seems like this method should work. The one thing I would add is an elvis operator in case the value is not in the session scope. For example, the CFML Session could expire but the user might still be connected to the websocket, so

return arguments.subscriber.getSessionScope().user.label ?: "";

The ConnectionManager already does that for you, taking into account synchronization, etc, so I think that your first approach is better.