Files
IRC-kosmi-relay/chat-summaries/2025-10-31_11-53-00_websocket-mutation-issue-http-solution.md
2025-10-31 16:17:04 -04:00

67 lines
2.0 KiB
Markdown

# WebSocket Mutation Issue - HTTP POST Solution
**Date**: October 31, 2025, 11:53 AM
**Issue**: IRC→Kosmi messages not appearing despite successful WebSocket send
## Problem Discovery
Messages from IRC were being sent to Kosmi's WebSocket successfully (we could see them in logs), but they were NOT appearing in the Kosmi chat interface.
### Root Cause
Through comprehensive logging of browser console messages, we discovered:
1. **WebSocket closes immediately after sending mutation**:
```
[Browser Console] >>> Sending mutation...
[Browser Console] >>> Sent successfully
[Browser Console] error: CloseEvent ← WebSocket closes!
```
2. **The WebSocket reopens** - indicating Kosmi is detecting an invalid message and resetting the connection
### Why WebSocket Mutations Fail
We're piggy-backing on Kosmi's native WebSocket connection (established by the web page). When we inject our own GraphQL mutations:
- We don't have proper authentication in the WebSocket frame
- We're interfering with Kosmi's protocol state machine
- The server detects this and closes the connection
## Solution: HTTP POST for Mutations
From FINDINGS.md (which was created earlier but we forgot about):
**Kosmi supports HTTP POST for GraphQL mutations!**
```
POST https://engine.kosmi.io/
Content-Type: application/json
{
"query": "mutation SendMessage($body: String!, $roomID: ID!) { sendMessage(body: $body, roomID: $roomID) { id } }",
"variables": {
"body": "message text",
"roomID": "room-id"
}
}
```
### Architecture
- **Receiving (Subscriptions)**: Use WebSocket ✅ (working)
- **Sending (Mutations)**: Use HTTP POST ✅ (to be implemented)
This is the same approach we initially documented but forgot to use!
## Implementation Plan
1. Replace `SendMessage` in `native_client.go` to use HTTP POST
2. Extract cookies from Playwright page context for authentication
3. Use Go's `http.Client` to send the POST request
4. Keep WebSocket for receiving messages (already working)
## Next Steps
Implement HTTP POST sending in the next iteration.