ispectify_ws 4.3.1-dev02 copy "ispectify_ws: ^4.3.1-dev02" to clipboard
ispectify_ws: ^4.3.1-dev02 copied to clipboard

An additional package for ws (logging and handling).

WebSocket interceptor integration for ISpectify logging system using ws package

pub version License: MIT GitHub stars

Pub likes Pub points

πŸ” Overview #

ISpectify WebSocket provides seamless integration between the ws package and the ISpectify logging system for comprehensive WebSocket monitoring.

πŸ”— WebSocket Logging β€’ πŸ“¨ Message Tracking β€’ ❌ Error Handling β€’ ⚑ Performance

Enhance your WebSocket debugging workflow by automatically capturing and logging all WebSocket client interactions using the ws package. Provides seamless integration with Dart's WebSocket client through interceptors for comprehensive connection and message monitoring.

🎯 Key Features #

  • πŸ”— WebSocket Connection Logging: Automatic logging of all WebSocket connections
  • πŸ“¨ Message Tracking: Detailed logging of sent and received messages
  • ❌ Error Handling: Comprehensive error logging with stack traces
  • πŸ” Connection Inspection: URL, connection state, and metrics logging
  • πŸ”’ Sensitive Data Redaction: Centralized redaction for sent/received payloads (enabled by default, configurable)
  • ⚑ Performance Metrics: Connection timing and message count tracking
  • πŸŽ›οΈ Lightweight: Minimal overhead using ws package interceptors

πŸ”§ Configuration Options #

Basic Setup #

final logger = ISpectify();

final interceptor = ISpectWSInterceptor(
  logger: logger,
  settings: const ISpectWSInterceptorSettings(
    enabled: true,
    printSentData: true,
    printReceivedData: true,
    printReceivedMessage: true,
    printErrorData: true,
    printErrorMessage: true,
    printReceivedHeaders: false,
  ),
);

final client = WebSocketClient(
  WebSocketOptions.common(
    interceptors: [interceptor],
  ),
);

interceptor.setClient(client);

Sensitive Data Redaction #

Redaction is enabled by default. Disable globally via settings or provide a custom redactor.

// Disable redaction
final interceptor = ISpectWSInterceptor(
  logger: logger,
  settings: const ISpectWSInterceptorSettings(enableRedaction: false),
);

// Provide a custom redactor
final redactor = RedactionService();
redactor.ignoreKeys(['x-debug']);
redactor.ignoreValues(['sample-token']);
final interceptor2 = ISpectWSInterceptor(
  logger: logger,
  redactor: redactor,
);

Advanced Configuration with Filters #

final interceptor = ISpectWSInterceptor(
  logger: logger,
  settings: ISpectWSInterceptorSettings(
    enabled: true,
    sentFilter: (request) {
      return request.body?['data']?.toString().contains('important') ?? false;
    },
    receivedFilter: (response) {
      return !response.body?['data']?.toString().contains('error') ?? true;
    },
    errorFilter: (error) {
      return true;
    },
    sentPen: AnsiPen()..blue(),
    receivedPen: AnsiPen()..green(),
    errorPen: AnsiPen()..red(),
  ),
);

Connection Event Handling #

final interceptor = ISpectWSInterceptor(
  logger: logger,
  onClientReady: (client) {
    print('WebSocket client is ready');
    print('Client metrics: ${client.metrics}');
  },
);

πŸ“¦ Installation #

Add ispectify_ws to your pubspec.yaml:

dependencies:
  ispectify_ws: ^4.3.1-dev02

πŸš€ Quick Start #

import 'dart:async';
import 'dart:io' as io show exit;
import 'package:ispectify/ispectify.dart';
import 'package:ispectify_ws/ispectify_ws.dart';
import 'package:ws/ws.dart';

void main() {
  const url = 'wss://echo.websocket.org';
  final logger = ISpectify();

  // Create WebSocket interceptor
  final interceptor = ISpectWSInterceptor(
    logger: logger,
    settings: const ISpectWSInterceptorSettings(
      enabled: true,
      printSentData: true,
      printReceivedData: true,
      printErrorData: true,
    ),
  );

  // Create WebSocket client with interceptor
  final client = WebSocketClient(
    WebSocketOptions.common(
      connectionRetryInterval: (
        min: const Duration(milliseconds: 500),
        max: const Duration(seconds: 15),
      ),
      interceptors: [interceptor],
    ),
  );

  // Set client for interceptor
  interceptor.setClient(client);

  // Connect and send messages - all will be automatically logged
  client
    ..connect(url)
    ..add('Hello WebSocket!')
    ..add('{"type": "message", "data": "JSON data"}');

  // Listen to messages
  client.stream.listen(
    (message) {
      print('Received: $message');
    },
    onError: (error) {
      print('Error: $error');
    },
  );

  // Close connection after some time
  Timer(const Duration(seconds: 5), () async {
    await client.close();
    print('Connection closed');
    print('Metrics: ${client.metrics}');
  });
}

πŸ“š Examples #

See the example/ directory for complete integration examples with different WebSocket client configurations.

πŸ—οΈ Architecture #

ISpectifyWS integrates with the WebSocket client through interceptors:

Component Description
WS Interceptor Captures WebSocket connection events and messages
Message Logger Logs sent and received message details
Connection Logger Logs connection state and URL information
Error Handler Captures and logs WebSocket errors
Metrics Tracker Measures connection timing and message counts

🀝 Contributing #

Contributions are welcome! Please read our contributing guidelines and submit pull requests to the main branch.

πŸ“„ License #

This project is licensed under the MIT License - see the LICENSE file for details.

  • ispectify - Foundation logging system
  • ispectify_dio - Dio HTTP client integration
  • ispectify_http - HTTP client integration
  • ispect - Main debugging interface
  • ws - WebSocket client package for Dart

Built with ❀️ for the Flutter community

0
likes
0
points
702
downloads

Publisher

verified publishershodev.live

Weekly Downloads

An additional package for ws (logging and handling).

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

ispectify, ws

More

Packages that depend on ispectify_ws