How to get all the connected users without using BaseNamespace?

80 views
Skip to first unread message

Yifu Diao

unread,
Dec 1, 2012, 5:52:20 PM12/1/12
to gevent-...@googlegroups.com
Hi, 
Is there a way to get all connected users(sockets) from a backend job? Right now I use recv_disconnect in BaseNamespace. It works but often delays, and resulting in a wrong online status of users. So I am thinking about using a cron job to run python scripts that can know all the online users and correct any invalid status. Kind of like self.socket.server.sockets in BaseNamespace, but not tied to BaseNamespace.

Thanks
Yifu

Joao Maciel

unread,
Aug 13, 2014, 9:34:59 AM8/13/14
to gevent-...@googlegroups.com
Hi,

What I did was:
- I created an array outside my BaseNamespace class, like _connections = []
- Override the initialize function to append every new connection to this array
    def initialize(self, *args, **kwargs):
        try:
            if self.request.user and self.request.user.is_active:
                _connections.append(self)
                self.lift_acl_restrictions()
            else:
                return
        except Exception, e:
            logger.error("Error initialize socket - "+str(e))

- Override disconnect function to remove connections from array:
    def disconnect(self, *args, **kwargs):
        try:
            _connections.remove(self)
            super(NotificacoesNamespace, self).disconnect(*args, **kwargs)
        except Exception, e:
            logger.error("Error disconnect socket - "+str(e))

I don't know if I'm following the best practices, but it's working.
Hope it helps.
Reply all
Reply to author
Forward
0 new messages