Skip to content

Usage stats - Total connections #371

Answered by enisdenjo
bristoljon asked this question in Q&A
Discussion options

You must be logged in to vote

Quite easy, simply use the onConnect and onClose hooks:

import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';

const server = new WebSocketServer({
  port: 4000,
  path: '/graphql',
});

let connectedClients = 0;
useServer(
  {
    // ...
    onConnect: () => {
      connectedClients++;
    },
    onClose: () => {
      connectedClients--;
    },
  },
  server,
);

console.log('Listening to port 4000');

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by enisdenjo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #370 on June 17, 2022 12:25.