Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to count how many clients are in a django channels Group

I would like to know how to count the number of clients in a Django Channels Group in order to restrict the number of connected clients for example.

I tried to look in the code of Group object but I had no success.

Here my code:

import re
import json
from channels import Group
from channels.sessions import channel_session
from login import login


@channel_session
def ws_connect(message):

    print "Connected"


    if Group("guis").count() > 10: # NOT POSSIBLE

        Group("guis").add(message.reply_channel)
        message.reply_channel.send({'accept': True})

    else:
        message.reply_channel.send({'accept': True})
like image 993
Hangon Avatar asked Oct 30 '22 05:10

Hangon


1 Answers

I dug around the source code a little bit and found the group_channels method. Try:

len(Group('guis').channel_layer.group_channels('guis'))

I don't know if this is the right way to do it or if it will work for all backends but at least it is a starting point.

like image 106
feus4177 Avatar answered Dec 10 '22 16:12

feus4177