Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:A616567126 GPT WEB JAVA Discord WSS Communication

From Leeroopedia
Knowledge Sources
Domains Real_Time_Communication, WebSockets
Last Updated 2026-10-13 00:00 GMT

Overview

Communication pattern that maintains a persistent WebSocket connection to Discord's Gateway, using zlib compression, heartbeat/ack lifecycle, and op-code dispatch to receive real-time events.

Description

Discord's Gateway is a WebSocket Secure (WSS) endpoint that streams events (messages, interactions, presence) in real time. Unlike simple WebSocket usage, Discord requires zlib stream compression, heartbeat/ack keep-alive, identify/resume authentication, and event-type dispatch routing.

This repository connects to Discord's Gateway as a bot user to monitor Midjourney task results. Each Discord account maintains its own WSS connection via UserWebSocketStarter. When a Midjourney interaction completes, Discord sends a MESSAGE_CREATE or MESSAGE_UPDATE event, which is dispatched to type-specific handlers (ImagineSuccess, UpscaleSuccess, etc.).

Usage

Use this principle when:

  • Integrating with Discord's bot API for real-time event monitoring
  • Building WSS clients that require compression and heartbeat management
  • Receiving asynchronous results from long-running interactions

Theoretical Basis

Gateway Lifecycle:

1. Client → WSS Connect (zlib-stream encoding)
2. Server → op=10 HELLO (heartbeat_interval)
3. Client → op=1 HEARTBEAT (every interval)
4. Server → op=11 HEARTBEAT_ACK
5. Client → op=2 IDENTIFY (token, properties, presence)
6. Server → op=0 DISPATCH t=READY (session_id, resume_gateway_url)
7. Server → op=0 DISPATCH t=MESSAGE_CREATE (event data)
8. Client → handle dispatch via MessageListener

Key Properties:

  • Zlib compression: Binary frames are decompressed via ZlibDecompressor before JSON parsing
  • Heartbeat: Must send op=1 at the interval specified in HELLO; if no ACK before next interval, reconnect
  • Identify/Resume: First connection sends IDENTIFY; reconnection sends RESUME with session_id and sequence
  • Sequence tracking: Each dispatch event includes a sequence number s for resume support
  • Op-code dispatch: op=0 events are routed by t field (READY, MESSAGE_CREATE, etc.)

Practical Guide

The repository implements this in UserWebSocketStarter:

  1. start() creates a WebSocket connection with zlib-stream encoding and Discord-specific headers
  2. onBinaryMessage() decompresses frames, parses op-codes, and routes to handlers
  3. handleHello() starts the heartbeat scheduler at the server-specified interval
  4. doResumeOrIdentify() sends IDENTIFY (new) or RESUME (reconnect) based on session state
  5. handleDispatch() extracts the event type and delegates to UserMessageListener.onMessage()
  6. UserMessageListener filters by channel_id and dispatches to registered MessageHandler implementations

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment