How to count the number of clients in django Group channels

I would like to know how to count the number of clients in a Django channel group to limit the number of connected clients, eg.

I tried to view the code of the Group object, but I had no success.

Here's 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})

      

+3


source to share


1 answer


I searched the source code a bit and found a method group_channels

. Try:

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

      



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

+1


source







All Articles