Expand description
§djs-gateway
A Discord gateway (WebSocket) client with transport compression — part of djs-rs.
Status: alpha. Self-healing shards, sharding with a shared IDENTIFY queue, the gateway send limit and zstd/zlib transport compression. The API may still change between alpha releases; see the changelog.
discord.js counterpart: @discordjs/ws
§Highlights
- Poll-driven.
shard.next_event().awaitdrives the connection: identify/resume, heartbeats with jitter, zombie detection, Hello timeout, exponential reconnect backoff and close-code handling all happen while you wait for the next event. No background tasks, no internal channels. - zstd-stream compression by default (feature
zstd-stream), or zlib-stream in pure Rust (featurezlib-stream), with a decompression buffer that is reused across messages and released after oversized payloads. - Sharding.
create_shards(range, total, config)creates the shards of a bot; they take turns identifying through a sharedIdentifyQueue(LocalIdentifyQueuehonoursmax_concurrency; implement the trait to coordinate several processes). Run each shard in its own task. - Send limit. Commands sent through a
MessageSenderare paced under Discord’s 120-per-minute limit and wait in order instead of getting the connection closed. - Single-pass decoding via
djs_model’sGatewayEventDeserializer. - tokio-websockets + rustls with an explicit ring provider and Mozilla roots, so it never panics because another crate enabled a different rustls provider.
§Example
use djs_gateway::Shard;
use djs_model::gateway::{DispatchEvent, GatewayEvent, Intents, ShardId};
let token = std::env::var("DISCORD_TOKEN").unwrap();
let mut shard = Shard::new(
ShardId::ONE,
token,
Intents::GUILDS | Intents::GUILD_MESSAGES | Intents::MESSAGE_CONTENT,
);
while let Some(event) = shard.next_event().await {
match event {
Ok(GatewayEvent::Dispatch(_, DispatchEvent::MessageCreate(message))) => {
println!("{}: {}", message.author.username, message.content);
}
Ok(_) => {}
Err(error) if error.is_fatal() => break,
Err(error) => eprintln!("gateway error: {error}"),
}
}§Cargo features
| Feature | Default | Description |
|---|---|---|
zstd-stream | ✅ | zstd-stream transport compression (needs a C compiler) |
zlib-stream | zlib-stream transport compression with the pure-Rust zlib-rs (no C compiler; compresses less and decodes more slowly than zstd, but uses less memory per shard) | |
native-roots | Also trust the operating system’s root certificates (e.g. a corporate proxy’s CA), in addition to the bundled Mozilla roots |
§Part of djs-rs
djs-gateway is one crate of djs-rs, a high-performance
Rust library for building Discord bots modelled on discord.js v14. It can be used on its own
or through the main djs-rs crate.
§License
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Structs§
- Config
- The configuration of a
Shard. - Config
Builder - Builds a
Config. Created byConfig::builder. - Local
Identify Queue - The in-process
IdentifyQueue: one IDENTIFY per rate-limit bucket every 5 seconds. - Message
Sender - Sends commands to a
Shardfrom any task. Cheap to clone. - Receive
Error - An error returned by
Shard::next_event. - Shard
- A single connection to the Discord gateway.
Enums§
- Compression
- Transport compression applied by Discord to everything it sends on the connection.
- Receive
Error Kind - The kind of a
ReceiveError. - Send
Error - An error returned by
MessageSender. - Shard
State - The connection state of a
Shard.
Constants§
- DEFAULT_
GATEWAY_ URL - The default gateway URL, used until a URL from Get Gateway Bot is provided.
Traits§
- Identify
Queue - Decides when a shard may send its IDENTIFY payload.
Functions§
- create_
shards - Creates the shards numbered
rangeof a bot that runstotalshards.