WebSocket: readyState property
The readyState property indicates the stage the connection is in right now.
Value
A number, one of the following four:
| Value | Constant | Meaning |
|---|---|---|
0 | WebSocket.CONNECTING | Connecting; messages cannot be sent yet |
1 | WebSocket.OPEN | Connected; messages can be sent and received |
2 | WebSocket.CLOSING | Closing |
3 | WebSocket.CLOSED | Ended |
- Read-only. Assigning to it throws
TypeError. - A new connection is
0. - It only moves in the direction
0→1→2→3and never goes back. When the connection fails or drops partway through, it goes straight from0or1to3.
Observing changes
- When it becomes
1,openfires. - When it becomes
3,closefires. - It becomes
2immediately afterclose()is called; this change has no event, because it is caused by the app's own call.
Examples
js
const ws = new WebSocket('wss://echo.websocket.org');
ws.addEventListener('open', () => {
console.log(ws.readyState === WebSocket.OPEN);
ws.close();
console.log(ws.readyState === WebSocket.CLOSING);
});Result: once connected, logs true, then true.
Differences from the web
The same as on the web.