Skip to content

GatewaySession

Defined in: packages/ws/src/session.ts:55

Session state for one Gateway shard.

Generations. Every connection attempt takes a token from GatewaySession.beginConnection, and every mutation must present it. A socket that has been superseded still holds the old token, so a late dispatch arriving on it cannot advance the new connection’s sequence or overwrite its session id. Ownership is therefore structural rather than a chain of if (this.#ws === ws) checks at each call site.

new GatewaySession(): GatewaySession

GatewaySession

get canResume(): boolean

Defined in: packages/ws/src/session.ts:104

Whether a RESUME is possible: a session, a resume host, and a sequence.

boolean


get generation(): number

Defined in: packages/ws/src/session.ts:75

Token of the current connection attempt; mutations must match it.

number


get lastInvalidation(): InvalidationReason | undefined

Defined in: packages/ws/src/session.ts:70

Why the last session was discarded, for diagnostics and reconnect policy.

InvalidationReason | undefined


get resumeURL(): string | undefined

Defined in: packages/ws/src/session.ts:87

Host to reconnect to for a RESUME, or undefined when none is known.

string | undefined


get sequence(): number | null

Defined in: packages/ws/src/session.ts:99

Last sequence number seen, or null when none has been observed.

Null, never zero, marks “nothing seen”: sequence 0 is a legitimate value that must survive into a RESUME.

number | null


get sessionId(): string | undefined

Defined in: packages/ws/src/session.ts:80

Discord’s session id, or undefined when no session is established.

string | undefined

activate(payload, token): boolean

Defined in: packages/ws/src/session.ts:141

Establishes the session from a READY payload.

unknown

number

boolean

Whether the token owned the session and READY was well-formed.


beginConnection(): number

Defined in: packages/ws/src/session.ts:114

Opens a new connection generation and returns its token.

The session itself is untouched: reconnecting to resume must keep the session id and sequence. Only ownership moves.

number


connectURL(fallback): string

Defined in: packages/ws/src/session.ts:202

Host the next connection should dial: the resume host, else fallback. The resume host comes from READY, so it is used only when it is a wss: URL on discord.gg (or a subdomain), or on the fallback’s own host; any other host would receive the bot token in the RESUME frame.

string

string


handshake(): HandshakeIntent

Defined in: packages/ws/src/session.ts:191

What the next connection must send: RESUME with material, or IDENTIFY.

HandshakeIntent


invalidate(token, reason?): boolean

Defined in: packages/ws/src/session.ts:169

Discards the session so the next connection must IDENTIFY.

Resumable invalidation (op 9 with d: true) is not an invalidation: the session survives and the caller simply reconnects, so this is only called for the non-resumable cases.

number

InvalidationReason = "explicit"

boolean

Whether the token owned the session and state was discarded.


owns(token): boolean

Defined in: packages/ws/src/session.ts:119

Whether token still owns this session.

number

boolean


recordSequence(sequence, token): boolean

Defined in: packages/ws/src/session.ts:127

Records a sequence number from a payload.

number

number

boolean

Whether the token owned the session and the value was stored.


resumeInfo(): ResumeInfo | undefined

Defined in: packages/ws/src/session.ts:183

Resume material, or undefined when a RESUME is not possible.

ResumeInfo | undefined