GraphQL subscriptions rely on WebSockets to provide real-time updates.
Two important lifecycle hooks in this process are onConnect and onDisconnect.
This article clearly explains:
- What
onConnectandonDisconnectare - Why they are needed
- When and how they are used
- Real-world use cases
What Are onConnect and onDisconnect?
In GraphQL subscriptions:
onConnectruns when a client establishes a WebSocket connectiononDisconnectruns when a client disconnects from the WebSocket
Think of them as entry and exit points of a real-time connection.
What Is onConnect in GraphQL Subscriptions?
Definition
onConnect is a callback function that executes when a client successfully connects to the GraphQL subscription server via WebSocket.
What Happens During onConnect?
When a client connects:
- WebSocket connection is opened
- Authentication data is sent (e.g., JWT token)
onConnectis triggered on the server- Server validates the client
- Context is created for subscriptions
Why onConnect Is Important
onConnect is mainly used for:
- Authentication & authorization
- Validating user identity
- Initializing connection-level data
- Logging active connections
π Without onConnect, subscriptions would be insecure.
Real-World Example of onConnect
Example use cases:
- Validate JWT token
- Identify logged-in user
- Reject unauthorized users
- Track online users
Simple analogy:
onConnect is like checking an ID card before entering a secure building.
What Is onDisconnect in GraphQL Subscriptions?
Definition
onDisconnect is a callback function that runs when a client disconnects from the WebSocket server.
What Happens During onDisconnect?
When a client disconnects:
- User closes browser or app
- Network connection drops
- WebSocket closes
onDisconnectis triggered- Server cleans up resources
Why onDisconnect Is Important
onDisconnect is used to:
- Release server resources
- Stop listening to events
- Update user online/offline status
- Clean up memory
π Without this, servers may leak resources.
Real-World Example of onDisconnect
Common use cases:
- Mark user as offline
- Remove user from active connections list
- Stop background listeners
- Log disconnection events
Simple analogy:
onDisconnect is like signing out when leaving the building.
When Are onConnect and onDisconnect Triggered?
| Event | Hook Triggered |
|---|---|
| WebSocket connection established | onConnect |
| WebSocket connection closed | onDisconnect |
| Browser tab closed | onDisconnect |
| Internet lost | onDisconnect |
Where Do onConnect and onDisconnect Fit in Subscription Lifecycle?
- Client opens WebSocket connection
- onConnect runs
- Client starts subscriptions
- Server sends real-time updates
- Client disconnects
- onDisconnect runs
Common Interview Question
Q: Can onConnect reject a client connection?
Answer:
Yes. If authentication fails inside onConnect, the server can reject the WebSocket connection and prevent subscriptions.
Best Practices
- Always validate authentication in
onConnect - Keep
onConnectlightweight - Clean up all resources in
onDisconnect - Log connections for debugging
- Handle unexpected disconnects
When Should You Use onConnect and onDisconnect?
Use them when:
- Authentication is required for subscriptions
- You need user presence tracking
- You want secure real-time communication
- You need connection-level context
Final Summary
| Hook | Purpose |
|---|---|
onConnect | Authenticate & initialize WebSocket connection |
onDisconnect | Cleanup & handle connection termination |
onConnect = entry gate
onDisconnect = exit gate
Understanding these two hooks is essential for building secure, scalable real-time GraphQL applications.