Important Interview Questions on onConnect and onDisconnect in GraphQL Subscriptions

GraphQL subscriptions use WebSockets to provide real-time data.
Two important lifecycle hooks in this process are onConnect and onDisconnect.

Interviewers often ask questions about these hooks to test your understanding of:

  • WebSocket lifecycle
  • Subscription security
  • Real-time system design

Let’s cover the most important interview questions with clear answers.


1. What Are onConnect and onDisconnect in GraphQL Subscriptions?

Answer:

  • onConnect is executed when a client establishes a WebSocket connection.
  • onDisconnect is executed when the client disconnects from the WebSocket.

They act as entry and exit points of a GraphQL subscription connection.


2. Why Are onConnect and onDisconnect Important?

Answer:
They are important for:

  • Authentication and authorization
  • Connection initialization
  • Resource cleanup
  • Tracking active users

Without these hooks, real-time applications can become insecure and inefficient.


3. When Is onConnect Triggered?

Answer:
onConnect is triggered:

  • After the WebSocket connection is opened
  • Before any subscription starts

It runs only once per WebSocket connection.


4. When Is onDisconnect Triggered?

Answer:
onDisconnect is triggered when:

  • The user closes the browser or app
  • Network connection is lost
  • The client explicitly disconnects

It ensures proper cleanup of server resources.


5. What Is Commonly Done Inside onConnect?

Answer:
Typical operations include:

  • Validating JWT or auth token
  • Identifying the user
  • Creating subscription context
  • Rejecting unauthorized connections

6. Can onConnect Reject a Connection?

Answer:
Yes.
If authentication fails, onConnect can:

  • Throw an error
  • Reject the WebSocket connection
  • Prevent subscriptions from starting

This is a common security practice.


7. What Is Commonly Done Inside onDisconnect?

Answer:
Typical tasks include:

  • Cleaning up memory or listeners
  • Removing user from active connections list
  • Updating online/offline status
  • Logging disconnection events

8. Is onDisconnect Always Called?

Answer:
In most cases, yes.
However, in sudden network failures, it may not fire immediately.

That’s why servers often use:

  • Heartbeats
  • Timeout detection

9. How Are onConnect and Subscriptions Related?

Answer:

  • onConnect runs before any subscription starts
  • It prepares the context used by subscription resolvers

If onConnect fails, subscriptions will not work.


10. Can Multiple Subscriptions Share the Same onConnect?

Answer:
Yes.
One WebSocket connection can handle multiple subscriptions, and onConnect runs only once for that connection.


11. Where Is Authentication Best Handled for Subscriptions?

Answer:
Authentication is best handled in onConnect, not in each subscription resolver.

Reason:

  • Authentication happens once
  • Reduces repeated validation
  • Improves performance

12. What Is the Difference Between HTTP Auth and Subscription Auth?

Answer:

FeatureHTTPWebSocket
Auth timingEvery requestOnce at connection
Auth hookMiddlewareonConnect
ConnectionStatelessStateful

13. Can onDisconnect Access User Information?

Answer:
Yes.
If user data was stored during onConnect, it can be accessed during onDisconnect to perform cleanup or logging.


14. What Happens If onConnect Is Not Implemented?

Answer:

  • Subscriptions may still work
  • But security, context, and connection control will be missing

In production apps, onConnect is strongly recommended.


15. Are onConnect and onDisconnect Client-Side or Server-Side?

Answer:
They are server-side lifecycle hooks and are part of the WebSocket subscription server configuration.


16. How Do onConnect and onDisconnect Help in Scaling?

Answer:
They help by:

  • Tracking active connections
  • Cleaning unused resources
  • Preventing memory leaks

This is critical for high-traffic real-time systems.


Interview Tip (Very Important)

onConnect = authenticate & initialize
onDisconnect = cleanup & finalize

If you remember this, you can confidently answer most interview questions.


Final Summary Table

HookPurpose
onConnectAuthentication & context setup
onDisconnectResource cleanup & status update