In most cases, performing REST operations on Discord resources can be done using the HTTP API rather than the Gateway API.
Gateway Events
Gateway events are payloads sent over a Gateway connection—either from an app to Discord, or from Discord to an app. An app typically sends events when connecting and managing its connection to the Gateway, and receives events when listening to actions taking place in a server. All Gateway events are encapsulated in a Gateway event payload. A full list of Gateway events and their details are in the Gateway events documentation.Example Gateway Event
Sending Events
When sending a Gateway event (like when performing an initial handshake or updating presence), your app must send an event payload object with a valid opcode (op) and inner data object (d).
Specific rate limits are applied when sending events, which you can read about in the Rate Limiting section.
- Must be serialized in plain-text JSON or binary ETF.
- Must not exceed 4096 bytes. If an event payload does exceed 4096 bytes, the connection will be closed with a
4002close event code.
Receiving Events
Receiving a Gateway event from Discord (like when a reaction is added to a message) is much more common (and slightly more complex) than sending them. While some events are sent to your app automatically, most events require your app to define intents when Identifying. Intents are bitwise values that can be ORed (|) to indicate which events (or groups of events) you want Discord to send your app. A list of intents and their corresponding events are listed in the intents section.
When receiving events, you can also configure how events will be sent to your app, like the encoding and compression, or whether sharding should be enabled).
All events that your app can receive via a connection are in the Gateway event documentation.
Dispatch Events
Dispatch (opcode0) events are the most common type of event your app will receive. Most Gateway events which represent actions taking place in a guild will be sent to your app as Dispatch events.
When your app is parsing a Dispatch event:
- The
tfield can be used to determine which Gateway event the payload represents the data you can expect in thedfield. - The
sfield represents the sequence number of the event, which is the relative order in which it occurred. You need to cache the most recent non-nullsvalue for heartbeats, and to pass when Resuming a connection.
Connections
Gateway connections are persistent WebSockets which introduce more complexity than sending HTTP requests or responding to interactions (like Slash Commands). When interacting with the Gateway, your app must know how to open the initial connection, as well as maintain it and handle any disconnects.Connection Lifecycle
There are nuances that aren’t included in the overview below. More details about each step and event can be found in the individual sections below.