Gateway
The gateway is managed by Client. Application code normally interacts with gateway traffic by implementing EventHandler.
Lifecycle
When client.start().await runs, the framework:
- validates the
Token; - builds a shared
BotApi; - fetches bot info with
get_bot_info; - fetches gateway metadata with
get_gateway; - creates shard sessions from the returned URL, shard count, and session-start limit;
- connects each shard, authenticates, sends heartbeats, and forwards gateway dispatch payloads into the client event loop.
The client then parses each gateway dispatch into the relevant Rust model and calls the matching EventHandler method.
Heartbeat and reconnect
After HELLO, the runtime starts a heartbeat task using the server-provided heartbeat interval. Heartbeats carry the last received sequence number. If the socket closes, the runtime tries to resume with the cached session id and sequence number. Non-resumable close codes force a new identify; fatal identify errors stop that shard's reconnect loop and are logged by the session manager.
The session manager spaces shard starts according to session_start_limit.max_concurrency. This keeps reconnects from hammering the platform after a network failure.
Event dispatch
Known event names are parsed into typed payloads. Examples:
READY->ready(ReadySession)AT_MESSAGE_CREATE->message_create(ChannelReplySession)DIRECT_MESSAGE_CREATE->direct_message_create(DirectReplySession)GROUP_AT_MESSAGE_CREATE->group_message_create(GroupReplySession)C2C_MESSAGE_CREATE->c2c_message_create(C2CReplySession)MESSAGE_REACTION_ADD/MESSAGE_REACTION_REMOVE-> reaction callbacks- guild, channel, member, manage, audio, forum, and open-forum events -> their corresponding callbacks
Unknown event names are passed to unknown_event(UnknownEventSession) so newer platform events can still be observed.
Configuration knobs
The public knobs are intentionally small:
Client::new(token, intents, handler, is_sandbox)Client::with_config(token, intents, handler, timeout, is_sandbox)Intentscontrols which gateway event categories QQ sends.is_sandboxswitches REST and gateway discovery to the sandbox environment.