#discord #discord-api #twilight


Discord Gateway implementation for the Twilight ecosystem

44 releases (8 breaking)

0.8.4 Jan 11, 2022
0.8.2 Dec 27, 2021
0.7.1 Oct 29, 2021
0.6.0 Jul 31, 2021
0.2.5 Nov 29, 2020

#8 in WebSocket

Download history 264/week @ 2021-09-29 507/week @ 2021-10-06 462/week @ 2021-10-13 312/week @ 2021-10-20 280/week @ 2021-10-27 473/week @ 2021-11-03 625/week @ 2021-11-10 591/week @ 2021-11-17 444/week @ 2021-11-24 865/week @ 2021-12-01 478/week @ 2021-12-08 735/week @ 2021-12-15 515/week @ 2021-12-22 900/week @ 2021-12-29 399/week @ 2022-01-05 452/week @ 2022-01-12

2,339 downloads per month
Used in less than 6 crates

ISC license

51K SLoC


codecov badge discord badge github badge license badge rust badge

twilight-gateway is an implementation of Discord's sharding gateway sessions. This is responsible for receiving stateful events in real-time from Discord and sending some stateful information.

It includes two primary types: the Shard and Cluster.

The Shard handles a single websocket connection and can manage up to 2500 guilds. If you manage a small bot in under about 2000 guilds, then this is what you use. See the Discord docs for more information on sharding.

The Cluster is an interface which manages the health of the shards it manages and proxies all of their events under one unified stream. This is useful to use if you have a large bot in over 1000 or 2000 guilds.


There are a few usage examples located in the root of the twilight repository.



twilight-gateway supports serde_json and simd-json for deserializing and serializing events.


The simd-json feature enables simd-json support to use simd features of modern cpus to deserialize responses faster. It is not enabled by default.

To use this feature you need to also add these lines to <project root>/.cargo/config:

rustflags = ["-C", "target-cpu=native"]

you can also use this environment variable RUSTFLAGS="-C target-cpu=native".

twilight-gateway = { default-features = false, features = ["rustls", "simd-json"], version = "0.2" }


twilight-gateway has features to enable async-tungstenite and twilight-http's TLS features. These features are mutually exclusive. rustls is enabled by default.


The native feature enables async-tungstenite's tokio-native-tls feature as well as twilight-http's native feature which is mostly equivalent to using native-tls.

To enable native, do something like this in your Cargo.toml:

twilight-gateway = { default-features = false, features = ["native"], version = "0.2" }


The rustls feature enables async-tungstenite's tokio-rustls feature and twilight-http's rustls feature, which use rustls as the TLS backend.

This is enabled by default.


zlib is enabled with the feature compression and one of the two zlib features described below. Enabling any of the two features below will also enable compression. compression is enabled by default.

There are 2 zlib features zlib-stock and zlib-simd for the library to work one of them has to be enabled. If both are enabled it will use zlib-stock

zlib-stock enabled by default.

Enabling only zlib-simd will make the library use zlib-ng which is a modern fork of zlib that is faster and more effective, but it needs cmake to compile.


The tracing feature enables logging via the tracing crate.

This is enabled by default.


The metrics feature provides metrics information via the metrics crate. Some of the metrics logged are counters about received event counts and their types and gauges about the capacity and efficiency of the inflater of each shard.

This is disabled by default.


~518K SLoC