2.8 KiB
2.8 KiB
CRITICAL FIX: GraphQL Operation Order
Problem
The bot was only receiving ONE message then the WebSocket would die with "websocket: close 1006 (abnormal closure): unexpected EOF".
Root Cause
We were subscribing to messages BEFORE joining the room, which is the opposite of what the browser does.
Browser's Actual Sequence (from HAR analysis)
- connection_init
- ExtendedCurrentUserQuery
- (various other queries)
- [24] JoinRoom ← Join FIRST
- (many subscriptions)
- [57] RoomDisconnect ← Subscribe to disconnect events
- [58] MemberJoins
- [59] MemberLeaves
- [61] NewMessageSubscription ← Subscribe to messages LAST
Our Previous (Broken) Sequence
- connection_init ✅
- ExtendedCurrentUserQuery ✅
- NewMessageSubscription ❌ TOO EARLY!
- JoinRoom ❌ TOO LATE!
- (start listening)
Fixed Sequence
- connection_init
- ExtendedCurrentUserQuery
- JoinRoom ← Now FIRST
- RoomDisconnect subscription ← NEW! Handles disconnect events
- NewMessageSubscription ← Now AFTER joining
Changes Made
1. Reordered Operations
- Moved
JoinRoomto happen BEFORENewMessageSubscription - This matches the browser's behavior
2. Added RoomDisconnect Subscription
subscription RoomDisconnect($roomId: String!) {
roomDisconnect(id: $roomId) {
ok
__typename
}
}
This subscription is CRITICAL - it handles disconnect events from the server and prevents the socket from dying unexpectedly.
3. Added Socket Identification
- All Kosmi WebSocket logs now prefixed with
[KOSMI WEBSOCKET] - All Jackbox WebSocket logs now prefixed with
[JACKBOX WEBSOCKET] - Makes debugging much easier
Expected Behavior After Fix
- Bot joins room successfully
- Bot subscribes to disconnect events
- Bot subscribes to message feed
- Bot receives ALL messages continuously (not just one)
- WebSocket stays alive
- Bot can send messages
Testing
Rebuild and run:
go build
docker-compose build
docker-compose up -d
docker-compose logs -f matterbridge
Look for:
✅ Successfully joined roomSubscribing to room disconnect eventsSubscribing to message feed🎧 [KOSMI WEBSOCKET] Message listener started- Multiple
Received message from Kosmientries (not just one!) - NO
❌ [KOSMI WEBSOCKET] Error reading messageorwebsocket: close 1006
Why This Matters
The server expects clients to:
- Join the room first
- Subscribe to disconnect events
- Then subscribe to messages
If you subscribe to messages before joining, the server may:
- Only send one message as a "test"
- Then close the connection because you're not properly joined
- Or ignore subsequent messages
This is a common pattern in WebSocket APIs - you must establish your presence (join) before subscribing to events.