Connection
Guides, route-group overviews, and live schema entry points for the Plexichat backend.
Gateway connection flow
This page describes how to connect to the Plexichat gateway, authenticate, maintain heartbeats, and resume a session. The implementation lives in src/api/websocket/gateway.py, src/api/websocket/handlers/connection.py, and src/api/websocket/opcodes.py.
Before you begin
- You need a valid session or bot token.
- You need the gateway URL:
wss://api.plexichat.com/gateway(path/gateway). - Decide which intents you need. The default intents bitmask is
3178237;0disables all dispatch events.
Connect and receive HELLO
Open a WebSocket connection to
wss://api.plexichat.com/gateway.Wait for
HELLO(op: 10). The server sends it immediately afterwebsocket.accept():{"op": 10, "d": {"heartbeat_interval": 45000}}heartbeat_intervalis in milliseconds. The reference implementation uses45000(45 seconds). You must send aHEARTBEATat that interval.
Authenticate with IDENTIFY
Send IDENTIFY (op: 2) to authenticate a new session. You must identify before sending any other opcode except HEARTBEAT.
{
"op": 2,
"d": {
"token": "SESSION_OR_BOT_TOKEN",
"properties": {"os": "linux", "browser": "custom", "device": "custom"},
"compress": false,
"intents": 3178237
}
}
| Field | Required | Description |
|---|---|---|
token |
Yes | Session or bot token. The server verifies it through AuthManager.verify_token. |
properties |
No | Client hints such as os, browser, and device. Defaults to an empty object. |
compress |
No | If true, the server enables zlib compression for subsequent frames (Connection.enable_compression). Defaults to false. |
intents |
No | Bitmask of GatewayIntent values. Defaults to 3178237 (all non-privileged intents except SCHEDULED_EVENTS, THREADS, AUTOMOD, and AUDIT_LOG). Values must be an integer 0–4194303; otherwise the server closes with 4013 INVALID_INTENTS. |
If IDENTIFY succeeds, the server replies with a DISPATCH (op: 0) event where t is READY:
{"op": 0, "t": "READY", "s": 1, "d": {"v": 10, "user": {"id": "123"}, "guilds": [], "session_id": "abc", "resume_gateway_url": "wss://gateway.example.com"}}
Save session_id and the sequence number s. You need both to resume. resume_gateway_url contains the URL to use when resuming.
Error cases:
- Missing or invalid token → close
4004 AUTHENTICATION_FAILED. - Invalid
intentstype or range (including booleans, strings, or values above4194303) → close4013 INVALID_INTENTS. - Privileged intents without approval → close
4014 DISALLOWED_INTENTS(if enforced at the gateway layer). - Sending
IDENTIFYwhen already inREADYstate → close4005 ALREADY_AUTHENTICATED. Resume instead. - Exceeding the per-user connection cap → close
4008 RATE_LIMITED.
Maintain heartbeats
Send HEARTBEAT (op: 1) every heartbeat_interval milliseconds:
{"op": 1, "d": 42}
Set
dto the last sequence number you received, ornullif you have not received anyDISPATCHyet.The server replies with
HEARTBEAT_ACK(op: 11):{"op": 11}The server also tracks liveness in
Connection.is_alive. If you miss two consecutive heartbeat windows, the server closes with4009 SESSION_TIMED_OUT.
If heartbeats stop being acknowledged, reconnect. Do not queue heartbeats faster than the negotiated interval.
Resume a session
If the connection drops and the close code is resumable (see Close codes), you can resume.
Reconnect to the gateway and wait for
HELLO(op: 10).Send
RESUME(op: 6) with three required fields:{ "op": 6, "d": { "token": "SESSION_OR_BOT_TOKEN", "session_id": "PREVIOUS_SESSION_ID", "seq": 42 } }token— the same token you used to identify.session_id— thesession_idfrom theREADYpayload.seq— the last sequence number you received, as an integer>= 0(not a boolean).
If resume succeeds, the server sends
DISPATCHwitht: RESUMEDand replays missed events:{"op": 0, "t": "RESUMED", "s": 42, "d": {}}If resume fails, the server sends
INVALID_SESSION(op: 9):{"op": 9, "d": false}When
disfalse, the session is not resumable. Send a freshIDENTIFY(op: 2) to start a new session. Whendistrue, you can try to resume again immediately.
The server validates seq type and range. An invalid seq results in INVALID_SESSION with d: false.
Handle server-requested reconnects
The server can request a reconnect with RECONNECT (op: 7). When you receive it, reconnect and resume if the close code is resumable, or identify if the server also closes the connection.
Related signaling
After you are in READY state, you can also send:
PRESENCE_UPDATE(op: 3) – Update your presence.- Voice opcodes
20–26– Voice session signaling. - Typing opcodes
40TYPING_STARTand41TYPING_STOP. - Fabric opcodes
60–63for artifacts and64–67for PlexiSum workbooks. See Opcodes.
For standard and application close codes and whether each code is resumable, see Close codes.