Graph ql subscriptions#281
Merged
RUKAYAT-CODER merged 2 commits intoApr 28, 2026
Merged
Conversation
|
@Purity-Euphemia Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #266
Description
This PR implements comprehensive GraphQL subscriptions for TeachLink, enabling real-time data updates without polling. The implementation leverages Apollo Client with graphql-ws for efficient WebSocket communication and includes automatic reconnection, error recovery, connection state tracking, and production-ready UI components.
Problem Statement
TeachLink requires real-time data updates for notifications, feed updates, tipping, reputation changes, and user activity. Previous approach relied on polling, which is inefficient, has high latency, and increases server load.
Solution Overview
useSubscription,usePollableSubscription) for easy integrationChanges Made
📦 Dependencies Added
🎯 Core Implementation
1. Subscription Configuration (
src/lib/graphql/subscriptions.ts)Features:
2. useSubscription Hook (
src/hooks/useSubscription.ts)Main hook for managing GraphQL subscriptions with full lifecycle support
Features:
Additional Hooks:
useSubscriptionConnection()- Listen to connection state changesusePollableSubscription()- Fallback to polling when WS unavailable3. Pre-built Subscriptions (
src/lib/graphql/subscriptionQueries.ts)15+ ready-to-use subscription definitions:
NEW_POSTS_SUBSCRIPTION- New posts in topicPOST_COMMENTS_SUBSCRIPTION- Comments on postsUSER_NOTIFICATIONS_SUBSCRIPTION- User notificationsTIPPING_UPDATES_SUBSCRIPTION- Received tipsREPUTATION_UPDATES_SUBSCRIPTION- Reputation changesUSER_ACTIVITY_SUBSCRIPTION- User statusSTUDY_GROUP_UPDATES_SUBSCRIPTION- Group messagesLIVE_QUIZ_RESPONSES_SUBSCRIPTION- Quiz responsesSEARCH_RESULTS_SUBSCRIPTION- Search updatesFEED_UPDATES_SUBSCRIPTION- Feed changesTYPING_INDICATOR_SUBSCRIPTION- Typing indicatorsMESSAGE_STATUS_SUBSCRIPTION- Message deliveryBLOCKCHAIN_TRANSACTION_SUBSCRIPTION- Transaction statusPRESENCE_SUBSCRIPTION- Who's online4. SubscriptionProvider (
src/components/SubscriptionProvider.tsx)React context provider for Apollo Client
Exports:
SubscriptionProvider- Wrapper componentuseSubscriptionClient()- Access Apollo clientuseHasSubscriptionClient()- Check availability5. UI Components (
src/components/subscription/SubscriptionUI.tsx)Production-ready components for subscription state management
Components:
ConnectionStatusIndicator- Visual status indicatorConnectionStatusBanner- Prominent status bannerSubscriptionLoadingState- Loading wrapper with fallback UIRealtimeUpdateIndicator- Flash notification for updatesSubscriptionSkeleton- Loading skeleton placeholderFeatures:
6. Demo Page (
src/app/subscriptions-demo/page.tsx)Interactive demo showcasing all features:
7. Tests (
src/hooks/__tests__/useSubscription.test.ts)Comprehensive unit tests covering:
📚 Documentation
GRAPHQL_SUBSCRIPTIONS_GUIDE.md
Complete user guide including:
GRAPHQL_SUBSCRIPTIONS_IMPLEMENTATION.md
Technical implementation details including:
Acceptance Criteria
✅ Real-time data updates without polling
✅ WebSocket link setup
✅ useSubscription Hook
✅ Connection Lifecycle Handling
✅ Reconnection Logic
Usage Examples
Basic Real-Time Feed
With Connection Monitoring
With Polling Fallback
Setup Instructions
1. Environment Variables
Add to
.env.local:2. Wrap App with Provider
In
src/app/layout.tsx:3. Use in Components
Just import and use the hook:
Files Changed
New Files (8 files)
Modified Files
Total
Architecture
Testing
Demo Page
Visit
http://localhost:3000/subscriptions-demoto:Run Tests
npm run test -- src/hooks/__tests__/useSubscription.test.tsManual Testing
/subscriptions-demopageBrowser Support
✅ Chrome/Edge 96+
✅ Firefox 95+
✅ Safari 15+
✅ Mobile browsers (iOS Safari 15+, Chrome Android)
Requirements:
Performance
Bundle Size
@apollo/client: ~80KB gzippedgraphql-ws: ~12KB gzippedgraphql: ~15KB gzippedRuntime
Optimizations
Code Quality
Security Considerations
Acceptance by TeachLink Standards
Related Issues
Deployment Checklist
Future Enhancements
Possible improvements for future PRs: