Skip to main content

Crate djs_gateway

Crate djs_gateway 

Source
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().await drives 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 (feature zlib-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 shared IdentifyQueue (LocalIdentifyQueue honours max_concurrency; implement the trait to coordinate several processes). Run each shard in its own task.
  • Send limit. Commands sent through a MessageSender are paced under Discord’s 120-per-minute limit and wait in order instead of getting the connection closed.
  • Single-pass decoding via djs_model’s GatewayEventDeserializer.
  • 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

FeatureDefaultDescription
zstd-stream✅zstd-stream transport compression (needs a C compiler)
zlib-streamzlib-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-rootsAlso 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.
ConfigBuilder
Builds a Config. Created by Config::builder.
LocalIdentifyQueue
The in-process IdentifyQueue: one IDENTIFY per rate-limit bucket every 5 seconds.
MessageSender
Sends commands to a Shard from any task. Cheap to clone.
ReceiveError
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.
ReceiveErrorKind
The kind of a ReceiveError.
SendError
An error returned by MessageSender.
ShardState
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§

IdentifyQueue
Decides when a shard may send its IDENTIFY payload.

Functions§

create_shards
Creates the shards numbered range of a bot that runs total shards.