Skip to content

fix(realtime): implement event buffering for URLSessionWebSocket #752

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 30, 2025

Conversation

grdsdev
Copy link
Collaborator

@grdsdev grdsdev commented Jul 25, 2025

Summary

  • Enhanced URLSessionWebSocket implementation with comprehensive documentation and improved code organization
  • Improved error handling with better WebSocket close code mapping per RFC 6455
  • Fixed race condition: Implemented event buffering to prevent message loss when onEvent callback is not yet attached
  • Added proper buffer management with size limits and cleanup mechanisms

Changes Made

Documentation & Code Organization

  • Added comprehensive class and method documentation
  • Improved code formatting and readability
  • Enhanced error handling with detailed close code mapping

Event Buffering System

  • Problem: Events could be lost if received before onEvent callback was attached
  • Solution: Added event buffering mechanism that stores events and replays them when callback is set
  • Buffer limited to 100 events to prevent memory issues
  • Automatic buffer cleanup on connection close
  • Thread-safe implementation using existing LockIsolated pattern

Error Handling Improvements

  • Better error mapping to appropriate WebSocket close codes
  • Handles network timeouts, connection loss, and protocol errors
  • Graceful handling of unknown message types

Test Plan

  • Verify events are properly buffered when no callback is attached
  • Confirm buffered events are replayed when callback is set
  • Test buffer size limits work correctly
  • Validate thread safety of buffering mechanism
  • Ensure proper cleanup on connection close

🤖 Generated with Claude Code

@grdsdev grdsdev requested review from mandarini and o-santi July 25, 2025 17:38
@grdsdev grdsdev changed the title refactor(realtime): improve URLSessionWebSocket documentation and error handling refactor(realtime): enhance URLSessionWebSocket with improved documentation, error handling, and event buffering Jul 25, 2025
@grdsdev grdsdev changed the title refactor(realtime): enhance URLSessionWebSocket with improved documentation, error handling, and event buffering fix(realtime): implement event buffering for URLSessionWebSocket Jul 25, 2025
@coveralls
Copy link

coveralls commented Jul 25, 2025

Pull Request Test Coverage Report for Build 16605653147

Details

  • 0 of 101 (0.0%) changed or added relevant lines in 1 file are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.7%) to 77.516%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Sources/Realtime/WebSocket/URLSessionWebSocket.swift 0 101 0.0%
Files with Coverage Reduction New Missed Lines %
Sources/Realtime/WebSocket/URLSessionWebSocket.swift 3 0.0%
Totals Coverage Status
Change from base Build 16605636117: -0.7%
Covered Lines: 5485
Relevant Lines: 7076

💛 - Coveralls

@grdsdev grdsdev force-pushed the refactor/websocket-impl-improvements branch from 91538a2 to 4fd6166 Compare July 29, 2025 18:09
@grdsdev grdsdev requested a review from dshukertjr July 29, 2025 19:29
grdsdev and others added 2 commits July 29, 2025 16:34
…or handling

Enhanced the URLSessionWebSocket implementation with comprehensive documentation,
improved error handling, and better code organization for maintainability.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
Add event buffering mechanism to prevent message loss when onEvent callback
is not yet attached. Events are buffered and replayed when callback is set.

- Add eventBuffer to MutableState to store incoming events
- Modify _trigger to buffer events when onEvent is nil
- Update onEvent setter to replay buffered events when callback attached
- Implement 100-event buffer limit to prevent memory issues
- Clear buffer on connection close for proper cleanup

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@grdsdev grdsdev force-pushed the refactor/websocket-impl-improvements branch from 4fd6166 to b2f02d7 Compare July 29, 2025 19:34
@grdsdev grdsdev merged commit faf2d0e into main Jul 30, 2025
16 of 17 checks passed
@grdsdev grdsdev deleted the refactor/websocket-impl-improvements branch July 30, 2025 08:16
@supabase-releaser
Copy link

🎉 This PR is included in version 2.31.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants