WebSocket Inspector

Android

The WebSocket Inspector tracks WebSocket connections from your app: connection URLs, sent and received frames, and lifecycle events (open, closing, closed, error).

WebSocket capture is experimental and under active development. The client API below is available, but frame forwarding to the inspector is still being wired up.

OkHttp Integration

If you use OkHttp for WebSocket connections, Snapbug ships helpers in the OkHttp interceptor artifact (ai.snapbug:snapbug-okhttp-interceptor).

Send Messages

Use sendWithSnapbug() instead of send() to track outgoing frames:

import io.snapbug.sdk.okhttp.websocket.sendWithSnapbug
 
webSocket.sendWithSnapbug("Hello, server!")
webSocket.sendWithSnapbug(byteString)

Listen for Messages

Wrap your WebSocketListener in SnapbugWebSocket.Listener to track incoming frames and lifecycle events. The wrapper delegates every callback to your listener:

import io.snapbug.sdk.okhttp.websocket.SnapbugWebSocket
 
val client = OkHttpClient()
val request = Request.Builder().url("wss://example.com/ws").build()
 
val listener = object : WebSocketListener() {
    override fun onMessage(webSocket: WebSocket, text: String) {
        // Your message handling logic
    }
 
    override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
        // Connection closed
    }
}
 
client.newWebSocket(request, SnapbugWebSocket.Listener(listener, id = "ws-1"))

Event Types

Tracked events map to the SnapbugWebSocketEvent.Event enum:

EventDescription
OpenWebSocket connection established
SendMessageFrame sent to the server
ReceiveMessageFrame received from the server
ClosingServer initiated connection close
ClosedWebSocket connection closed
ErrorConnection failed with an error

Use a consistent id for the same WebSocket connection. This allows the inspector to group events by connection.