Skip to main content

WuKongIM Protocol

WuKongIM protocol is an efficient binary communication protocol designed specifically for instant messaging scenarios. This document provides detailed descriptions of packet structure, control types, and encoding specifications.

📋 Control Packet Structure

Each WuKongIM control packet consists of the following three parts:

🔧 Fixed Header

Each WuKongIM control packet contains a fixed header used to identify packet type and length information.

📝 WuKongIM Control Packet Types

WuKongIM protocol defines 10 different control packet types, each with specific purposes and data structures.

🏷️ Control Packet Flag Bits

Different protocol packets use different flag bits to control message behavior: Flag bits in Send and Recv protocols Flag bits in Connack protocol Flag bits in Chunk protocol
Flag bit descriptions:
  • DUP: Whether it’s a duplicate message (clients need to mark DUP as 1 when resending messages)
  • SyncOnce: Sync only once. In multi-device scenarios, if one device has pulled this message, other devices will not pull this message again (e.g., friend request messages)
  • RedDot: Whether the client should show a red dot when receiving the message
  • NoPersist: Whether not to store this message
  • Reserved: Reserved bit
  • HasServerVersion: Whether there is a server version number
  • End: Whether it’s the ending message chunk

📏 Remaining Length Encoding

The remaining length field indicates the number of bytes remaining in the current message, including variable header and payload (content). This is a variable-length encoding field.
Encoding rules:
  • Single byte maximum value: 01111111 (0x7F, 127)
  • The eighth bit (most significant bit) being 1 indicates there are subsequent bytes
  • Maximum of 4 bytes allowed to represent remaining length
  • Maximum length: 0xFF,0xFF,0xFF,0x7F = 268,435,455 bytes = 256MB
Byte ranges:
Encoding understanding:
  • 1st byte base: 1
  • 2nd byte base: 128 (2^7)
  • 3rd byte base: 128×128 = 2^14
  • 4th byte base: 128×128×128 = 2^21
Encoding example: Expressing 321 = 65 + 2×128 (2 bytes): 11000001 00000010
Byte order: The first byte is low-order, subsequent bytes are high-order, but within bytes, low-order is on the right, high-order is on the left.

🔤 String UTF-8 Encoding

WuKongIM uses a modified UTF-8 encoding format:

🔄 Variable Header

Some control packets contain a variable header section located between the fixed header and payload. The content of the variable header varies depending on the packet type.

📡 Protocol Packet Specifications

🔌 CONNECT Connection Packet

Packet format used when a client initiates a connection request to the server.

✅ CONNACK Connection Acknowledgment

CONNACK packet is sent by the server as a response to the client’s CONNECT packet. If the client doesn’t receive a CONNACK packet within a reasonable time, it should close the network connection.

📤 SEND Send Message

Packet format used when a client sends a message to the server.

✅ SENDACK Send Message Acknowledgment

Server acknowledgment response to client message sending.

📥 RECV Receive Message

Packet format used when the server pushes messages to the client.

✅ RECVACK Receive Message Acknowledgment

Client acknowledgment response to server message push.

🏓 PING

Heartbeat request packet sent by the client to keep the connection active.

🏓 PONG

Server response packet to client PING request.

🔌 DISCONNECT

Packet format used when client or server requests to disconnect.

⚙️ Message Settings

📊 Message Setting Bit Fields

Message settings are 1 byte (8 bits) used to control various message behavior characteristics.

🔧 Setting Bit Descriptions

Bit function descriptions:
  • Receipt: Message read receipt, this flag indicates this message requires read receipt
  • Reserved: Reserved bit, not yet used
  • Signal: Encryption flag
  • NoEncrypt: Whether message encryption is disabled
  • Topic: Whether message contains topic (if 1, both send and receive packets will include topic field)
  • Reserved: Reserved bit, not yet used
  • Stream: Stream message flag
  • Reserved: Reserved bit, not yet used

Complete Protocol Documentation: This document contains the core parts of the WuKongIM protocol. The complete protocol specification also includes recommended Payload structure, regular message format, system message format, and other detailed content. Please refer to the source documentation for complete information.