Count active subscriptions #2719
-
I'm trying to get a count of the number of active subscriptions in my Strawberry server. I have been unable to find how to do this. For reference I'm using an I've tried a variety of methods:
Is there something I've missed? Thanks for any help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @AlexanderWells-diamond, currently I can't think of an trivial way to achieve this. The aiohttp view creates a new instance of The code could look roughtly like this: from strawberry.aiohttp.handlers import GraphQLTransportWSHandler
from strawberry.aiohttp.views import GraphQLView
from aiohttp import web
some_global_counter = 0
class CustomHandler(GraphQLTransportWSHandler):
async def handle_request(self):
some_global_counter = some_global_counter + 1
await super().handle_request()
some_global_counter = some_global_counter - 1
class CustomView(GraphQLView):
graphql_transport_ws_handler_class = CustomHandler
schema = ... # your schema
app = web.Application()
app.router.add_route("*", "/graphql", GraphQLView(schema=schema)) Note that I haven't tested it, just wanted to give you a starting point to a potential solution. |
Beta Was this translation helpful? Give feedback.
Hey @AlexanderWells-diamond, currently I can't think of an trivial way to achieve this. The aiohttp view creates a new instance of
strawberry.aiohttp.handles.GraphQLTransportWSHandler
for each incoming ws connection using the graphql-transport-ws protocol. You could subclass said class and extend itshandle_request
method to keep track of individual connections by updating some global counter.The code could look roughtly like this: