Skip to content

feat: Add WebSocket support for URLSession, Starscream, and Apollo#354

Open
gtsifrikas wants to merge 1 commit into
kean:mainfrom
gtsifrikas:task/websocket_support
Open

feat: Add WebSocket support for URLSession, Starscream, and Apollo#354
gtsifrikas wants to merge 1 commit into
kean:mainfrom
gtsifrikas:task/websocket_support

Conversation

@gtsifrikas

Copy link
Copy Markdown

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
@kean

kean commented Jan 19, 2026

Copy link
Copy Markdown
Owner

Hey, thanks for the PR @gtsifrikas. I'm currently working on someting, but I will get to it as soon as I have a minute!

@FKREISE

FKREISE commented Apr 28, 2026

Copy link
Copy Markdown

@kean Did you have time to look into this yet? Having it at least for native URLSession web socket tasks (without the dependencies) would be amazing.

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.

3 participants