Skip to content

Commit 4cf9f1f

Browse files
authored
Merge pull request #1169 from multiversx/ws-error-handling
WS Subscriptions Error Handling
2 parents e3bdd80 + 07e2ebf commit 4cf9f1f

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

docs/sdk-and-tools/rest-api/ws-subscriptions.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,13 +364,76 @@ main().catch(console.error);
364364

365365
---
366366

367+
## Error Handling
368+
369+
Unexpected behaviors, such as sending an invalid payload or exceeding the server's subscription limits, will trigger an `error` event emitted by the server.
370+
371+
You should listen to this event to handle failures gracefully.
372+
373+
### Payload (DTO)
374+
375+
The error object contains context about which subscription failed and why.
376+
377+
| Field | Type | Description |
378+
|---------|--------|------------------------------------------------------------------------------------|
379+
| pattern | string | The subscription topic (event name) that was requested (e.g., `subscribePool`). |
380+
| data | object | The original payload sent by the client that caused the error. |
381+
| error | object | The specific error returned by the server. |
382+
383+
### Example usage
384+
385+
```js
386+
import { io } from "socket.io-client";
387+
388+
// ... setup socket connection ...
389+
390+
// Listen for generic errors from the server
391+
socket.on("error", (errorData) => {
392+
console.error("Received error from server:");
393+
console.dir(errorData, { depth: null });
394+
});
395+
```
396+
397+
### Error Example
398+
399+
**Scenario:** The client attempts to open more subscriptions than the server allows (e.g., limit of X).
400+
401+
```json
402+
{
403+
"pattern": "subscribePool",
404+
"data": {
405+
"from": 0,
406+
"size": 25,
407+
"type": "badInput"
408+
},
409+
"error": [
410+
{
411+
"target": {
412+
"from": 0,
413+
"size": 25,
414+
"type": "badInput"
415+
},
416+
"value": "badInput",
417+
"property": "type",
418+
"children": [],
419+
"constraints": {
420+
"isEnum": "type must be one of the following values: Transaction, SmartContractResult, Reward"
421+
}
422+
}
423+
]
424+
}
425+
```
426+
427+
---
428+
367429
## Summary
368430

369431
- WebSocket endpoint is dynamically obtained via `/websocket/config`.
370432
- Each stream has its own subscribe and update events.
371433
- Payload DTOs define allowed fields and required/optional rules.
372434
- Update messages mirror REST API and include `<resource>Count` fields.
373435
- `<resource>Count` reflects **total items at the moment of update**.
436+
- Errors are emitted via the standard `error` event, containing the pattern, original data, and error message.
374437
- Uses `socket.io-client`.
375438

376-
This document contains everything required to use MultiversX WebSocket Subscriptions effectively.
439+
This document contains everything required to use MultiversX WebSocket Subscriptions effectively.

0 commit comments

Comments
 (0)