Every message that flows between the browser and the Pokémon Showdown game server passes throughDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/smogon/pokemon-showdown-client/llms.txt
Use this file to discover all available pages before exploring further.
PSConnection, defined in client-connection.ts. The class is intentionally thin: it owns the socket, manages the reconnect loop, and delegates all higher-level message handling to PS.receive(). HTTP requests — for login, team storage, and the REST API — go through two separate helpers: the Net fetch wrapper and PSLoginServer.
PSConnection
socket
The underlying
WebSocket (or SockJS shim). null when a Web Worker is handling the connection, or when disconnected.connected
true once the socket handshake completes. While false, outgoing messages are queued in queue[].queue
Messages buffered before connection is established. Flushed in FIFO order immediately on connect.
reconnectDelay
Exponential back-off delay (ms). Starts at
1 000, doubles on each failed attempt, capped at 15 000.Connection lifecycle
Initialise
PSConnection.connect() is called at module load. It creates a new PSConnection instance (or calls reconnect() on an existing one) and triggers PSStorage.init() to load cross-origin localStorage data before opening the socket.Prefer a Web Worker
initConnection() calls tryConnectInWorker() first. If the browser supports Worker, a client-connection-worker.js worker is spun up and sent a { type: 'connect', server: PS.server } message. All socket I/O then runs off the main thread.Fall back to direct connect
If the worker fails (e.g. CSP, no Worker support),
directConnect() opens a SockJS or WebSocket connection on the main thread.Receive messages
Incoming data is forwarded to
PS.receive(data), which parses the room prefix and dispatches each protocol line to the appropriate PSRoom.tryConnectInWorker and directConnect
Web Worker message protocol
- Main → Worker
- Worker → Main
type | data field | Purpose |
|---|---|---|
'connect' | PS.server object | Open the socket to this server |
'send' | message string | Send a message over the socket |
canReconnect
Sending messages
PS.send(msg, roomid?) wraps PSConnection.send():
|. A message to the global room omits the prefix:
ServerInfo: the server config structure
TheServerInfo interface (defined in client-main.ts) describes which server to connect to. It is populated either from Config.defaultserver or from the cross-origin PSStorage iframe handshake:
PSLoginServer
PSLoginServer is an HTTP client for the PS authentication endpoint (/~~<serverid>/action.php). It handles the login flow (challenge-response with challstr) and other account actions.
Common actions
Net: the fetch wrapper
Net is a lightweight XMLHttpRequest wrapper used throughout the client when a full fetch API is unavailable or undesirable.
Usage examples
Net.defaultRoute can be set to a base URL so that paths starting with / are automatically prefixed. When running under file: protocol (the test client), Net automatically upgrades // URLs to https:.Helper utilities on Net
| Method | Signature | Purpose |
|---|---|---|
Net.encodeQuery | (data: string | PostData) => string | URL-encodes a key-value object into application/x-www-form-urlencoded format |
Net.formData | (form: HTMLFormElement) => Record<string, string | boolean> | Reads all named inputs from a form element |
