@lunibee/ws
v0.4.0
Published
> Discord Gateway (WebSocket) connection manager for the lunibee ecosystem.
Readme
@lunibee/ws
Discord Gateway (WebSocket) connection manager for the lunibee ecosystem.
Lunibee uses a single-connection Gateway (sharding lives in @lunibee/sharding). It
exposes a familiar surface (opcodes, close codes, lifecycle) while keeping
Lunibee's canonical names.
bun add @lunibee/wsBasic use
import { Gateway } from "@lunibee/ws";
import { GatewayIntentBits } from "@lunibee/types";
const gateway = new Gateway({
token: process.env.TOKEN!,
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
});
gateway.on("ready", (data) => console.log("Ready", data));
gateway.on("resumed", () => console.log("Resumed"));
gateway.on("close", (data) => console.log("Closed", data)); // { code, action }
gateway.on("error", (err) => console.error(err));
await gateway.connect(); // resolves once the socket opensEvery dispatch is also emitted under its Discord name (READY, MESSAGE_CREATE, …), plus
RAW, open, close, error, stateChange, heartbeatAck, invalidSession and zombie.
Exports
| Export | Kind | Notes |
|---|---|---|
| Gateway | class | Canonical connection manager. |
| GatewayOpcodes | const | Discord opcode names → values. |
| GatewayCloseCodes | const | Discord close-code names → values (AuthenticationFailed = 4004, InvalidSeq = 4007, …). |
| GatewayState | enum | Lunibee lifecycle states. |
| Status | alias | Alias of GatewayState. Values stay Lunibee strings. |
| GatewayError | class | Thrown/emitted on gateway failures. |
| GatewayOptions | interface | Connection config (compress, shardId/shardCount, reconnect tuning, createSocket). |
Lifecycle behaviour
- Heartbeats with initial jitter, ACK tracking, and a stale-ACK / zombie close guard.
- Reconnect with exponential backoff + jitter (
reconnectBaseDelay,reconnectMaxDelay,maxReconnectAttempts). - Resume vs re-identify is chosen from the close/opcode:
INVALID_SESSIONwithd: true→ keeps the session and RESUMEs;d: false→ clears it and re-IDENTIFYs against the main Gateway.4007/4009→ fresh IDENTIFY;4004/4010–4014→ fatal, stops reconnecting.
- A successful
RESUMEDresets the reconnect backoff and emitsresumed. - Send budget: application sends are capped at 115 per 60 s; heartbeats, IDENTIFY and RESUME always go out.
- Compression:
compress: truedecodes Discord'szlib-streamin arrival order.
Internally the gateway is split into transport, heartbeat, session, reconnect, protocol, decoder and send-budget modules.
What's new in 0.2.0
Reconnects that should resume close with 4900 (never 1000/1001), the heartbeat interval starts after the first jittered beat, and a lower sequence can never roll back the resume point.
