Skip to content

Conversation

@gtsifrikas
Copy link

Summary

This PR adds comprehensive WebSocket logging support to Pulse, enabling developers to inspect WebSocket traffic alongside regular HTTP requests.

Features

URLSession WebSocket Support

  • Extended URLSessionProxyDelegate with URLSessionWebSocketDelegate conformance
  • Added WebSocketTaskProxy to intercept send and receive operations
  • Updated URLSessionProxy with webSocketTaskProxy(with:) method for full logging

New WebSocket Event Types

  • webSocketTaskOpened: Logs when a WebSocket connection is established
  • webSocketTaskClosed: Logs connection closure with code and reason
  • webSocketFrameSent: Logs outgoing frames (text/binary)
  • webSocketFrameReceived: Logs incoming frames (text/binary)

Core Data Model Extensions

  • Added WebSocketFrameEntity for persistent storage of frames
  • Added webSocketFrames relationship to NetworkTaskEntity
  • Frame entities track type, data, direction, and timestamps

PulseStarscream Integration (new library target)

  • StarscreamLogger: WebSocketDelegate proxy for logging
  • Convenience extension: WebSocket.enablePulseLogging()
  • Logs connection lifecycle and frame data

PulseApollo Integration (new library target)

  • ApolloWebSocketLogger: WebSocketTransportDelegate proxy
  • Supports GraphQL subscription logging with JSON serialization
  • Handles Apollo's internal DataDict/SelectionSet types
  • Convenience extension: WebSocketTransport.enablePulseLogging()

UI Enhancements

  • WebSocketInspectorView: Timeline of frames with expandable details
  • WebSocket badge (WS) in console task cells
  • Task type filtering for WebSocket connections
  • JSON formatting for text frames

Demo App

  • Added WebSocketDemoView with three test scenarios:
    • URLSession WebSocket (echo.websocket.org)
    • Starscream WebSocket (echo.websocket.org)
    • Apollo WebSocket (simulated GraphQL subscription)
  • Tab-based UI with embedded Pulse console

Documentation

  • Updated DocC documentation with WebSocket usage examples
  • Added WebSocket section to Network Logging article

Usage Examples

URLSession

let session = URLSessionProxy(configuration: .default)
let wsTask = session.webSocketTaskProxy(with: url)
wsTask.resume()

try await wsTask.send(.string("Hello"))
let message = try await wsTask.receive()

Starscream

import PulseStarscream

let socket = WebSocket(request: request)
socket.enablePulseLogging(delegate: myDelegate)
socket.connect()

Apollo GraphQL

import PulseApollo

let transport = WebSocketTransport(websocket: ws, store: store)
transport.enablePulseLogging(delegate: self, url: url)

Testing

  • Builds successfully with swift build
  • Demo app builds and runs on iOS Simulator
  • Manual testing with echo.websocket.org

Breaking Changes

None - all changes are additive.

Dependencies Added

  • Starscream 4.0+ (for PulseStarscream target)
  • Apollo iOS 1.0+ (for PulseApollo target)

This PR adds comprehensive WebSocket logging support to Pulse:

## Core Features

### URLSession WebSocket Support
- Extended URLSessionProxyDelegate with URLSessionWebSocketDelegate conformance
- Added WebSocketTaskProxy to intercept send/receive operations
- Updated URLSessionProxy to return WebSocketTaskProxy instances

### New WebSocket Event Types
- webSocketTaskOpened: Logs when a WebSocket connection is established
- webSocketTaskClosed: Logs connection closure with code and reason
- webSocketFrameSent: Logs outgoing frames (text/binary)
- webSocketFrameReceived: Logs incoming frames (text/binary)

### Core Data Model Extensions
- Added WebSocketFrameEntity for persistent storage of frames
- Added webSocketFrames relationship to NetworkTaskEntity
- Frame entities track type, data, direction, and timestamps

### PulseStarscream Integration (new target)
- StarscreamLogger: WebSocketDelegate proxy for logging
- Convenience extension: WebSocket.enablePulseLogging()
- Logs connection lifecycle and frame data

### PulseApollo Integration (new target)
- ApolloWebSocketLogger: WebSocketTransportDelegate proxy
- Supports GraphQL subscription logging with JSON serialization
- Handles Apollo's internal DataDict/SelectionSet types
- Convenience extension: WebSocketTransport.enablePulseLogging()

### UI Enhancements
- WebSocketInspectorView: Timeline of frames with expandable details
- WebSocket badge (WS) in console task cells
- Task type filtering for WebSocket connections
- JSON formatting for text frames

### Demo App
- Added WebSocketDemoView with three test scenarios:
  - URLSession WebSocket (echo.websocket.org)
  - Starscream WebSocket (echo.websocket.org)
  - Apollo WebSocket (simulated GraphQL subscription)
- Tab-based UI with embedded Pulse console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant