-
Notifications
You must be signed in to change notification settings - Fork 71
Add websockets stream api docs #1074
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
franklywatson
merged 21 commits into
onflow:main
from
The-K-R-O-K:illia-malachyn/6644-new-websockets-stream-api-docs
Apr 21, 2025
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
82d333a
Add skeleton for new websockets stream api docs
illia-malachyn 935825a
change structure of pages
illia-malachyn 28c11d5
add some up-to-date info
illia-malachyn 3bb8e24
improve some explanations
k1nder10 16c701b
Added suported topics. Minor fixes
Guitarheroua 29b4230
fixed typos
Guitarheroua eea4d2f
Apply suggestions from code review
Guitarheroua b8cd59d
added supported values for block_status
Guitarheroua de5b5a2
Merge branch 'main' of github.com:The-K-R-O-K/docs into illia-malachy…
Guitarheroua 66e4f76
Moved to correct chapter. Restructure files
Guitarheroua 6c05b39
Merge branch 'onflow:main' into illia-malachyn/6644-new-websockets-st…
Guitarheroua a9c1115
Restructured supported topics. Added cross linking.
Guitarheroua d58780c
Improved examples for all bloks and events
Guitarheroua dee8107
improved account statuses topic documentation
Guitarheroua ab2a79f
improved ts statuses documentation topic documentation
Guitarheroua b3b9c8d
Added cross referance for topics
Guitarheroua 21c4bb7
added postman example
Guitarheroua 9c890cd
remove unnecessary files
Guitarheroua 0fe73ad
added common errors page
Guitarheroua 38df4b3
changed uuid to custom id
Guitarheroua 05a8f9b
Apply suggestions from code review
Guitarheroua File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
184 changes: 184 additions & 0 deletions
184
...de-ops/access-onchain-data/access-nodes/accessing-data/websockets-stream-api.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
--- | ||
title: Flow Websockets Stream API Specification | ||
sidebar_label: Websockets Stream API | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Flow Websockets Stream API Documentation | ||
|
||
## Overview | ||
|
||
The Stream API allows clients to receive real-time updates from the Flow blockchain via WebSocket connections. It | ||
supports subscribing to various topics, such as blocks, events, and transactions, enabling low-latency access to live | ||
data. | ||
|
||
### Important Information | ||
|
||
- **Endpoint**: The WebSocket server is available at: | ||
``` | ||
wss://api.flow.com/ws | ||
``` | ||
- **Limits**: | ||
- Each connection supports up to 50 concurrent subscriptions. Exceeding this limit will result in an error. | ||
- TODO: list all limits here | ||
- **Supported Topics**: | ||
- `blocks` | ||
- `block_headers` | ||
- `block_digests` | ||
- `events` | ||
- `transaction_statuses` | ||
- `account_statuses` | ||
- **Notes**: Always handle errors gracefully and close unused subscriptions to maintain efficient connections. | ||
|
||
--- | ||
|
||
## Setting Up a WebSocket Connection | ||
|
||
Use any WebSocket client library to connect to the endpoint. Below is an example using JavaScript: | ||
|
||
```javascript | ||
const ws = new WebSocket('wss://api.flow.com/ws'); | ||
illia-malachyn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
ws.onopen = () => { | ||
console.log('Connected to WebSocket server'); | ||
}; | ||
|
||
ws.onclose = () => { | ||
console.log('Disconnected from WebSocket server'); | ||
}; | ||
|
||
ws.onerror = (error) => { | ||
console.error('WebSocket error:', error); | ||
}; | ||
``` | ||
|
||
--- | ||
|
||
## Subscribing to Topics | ||
|
||
To receive data from a specific topic, send a subscription request in JSON format over the WebSocket connection. | ||
|
||
### Request Format | ||
|
||
```json | ||
{ | ||
"subscription_id": "some-uuid-42", | ||
"action": "subscribe", | ||
"topic": "blocks", | ||
"parameters": { | ||
"start_block_height": "123456789" | ||
} | ||
} | ||
``` | ||
|
||
- **`subscription_id`**: A unique identifier for the subscription (UUID string). If omitted, the server generates one. | ||
- **`action`**: The action to perform. Supported actions include: `subscribe`, `unsubscribe`, `list_subscriptions`. | ||
- **`topic`**: The topic to subscribe to. See the supported topics in the Overview. | ||
- **`parameters`**: Additional parameters for subscriptions, such as `start_block_height`, `start_block_id`, and others. | ||
|
||
### Response Format | ||
|
||
```json | ||
{ | ||
"subscription_id": "some-uuid-42", | ||
"error": null | ||
} | ||
``` | ||
|
||
--- | ||
|
||
## Unsubscribing from Topics | ||
|
||
To stop receiving data from a specific topic, send an unsubscribe request. | ||
|
||
### Request Format | ||
|
||
```json | ||
{ | ||
"subscription_id": "some-uuid-42", | ||
"action": "unsubscribe" | ||
} | ||
``` | ||
|
||
### Response Format | ||
|
||
```json | ||
{ | ||
"subscription_id": "some-uuid-42", | ||
"error": null | ||
} | ||
``` | ||
|
||
--- | ||
|
||
## Listing Active Subscriptions | ||
|
||
You can retrieve a list of all active subscriptions for the current WebSocket connection. | ||
|
||
### Request Format | ||
|
||
```json | ||
{ | ||
"action": "list_subscriptions" | ||
} | ||
``` | ||
|
||
### Response Format | ||
|
||
```json | ||
{ | ||
"subscriptions": [ | ||
{ | ||
"subscription_id": "uuid-1", | ||
"topic": "blocks", | ||
"parameters": { | ||
"start_block_height": "123456789" | ||
} | ||
}, | ||
{ | ||
"subscription_id": "uuid-2", | ||
"topic": "events", | ||
"parameters": {} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
--- | ||
|
||
## Errors Example | ||
|
||
If a request is invalid or cannot be processed, the server responds with an error message. | ||
|
||
### OK Response | ||
|
||
```json | ||
{ | ||
"subscription_id": "some-uuid-42", | ||
"payload": { | ||
"id": "0x1234...", | ||
"height:": "123456789", | ||
"timestamp": "2025-01-02T10:00:00Z" | ||
}, | ||
"error": null | ||
} | ||
``` | ||
|
||
### Error Response | ||
|
||
```json | ||
{ | ||
"subscription_id": "some-uuid-42", | ||
"payload": null, | ||
"error": { | ||
"code": 500, | ||
"message": "Access Node failed" | ||
} | ||
} | ||
``` | ||
|
||
### Common Error Codes | ||
|
||
- **400**: Invalid message format or parameters | ||
- **404**: Subscription not found | ||
- **500**: Internal server error |
48 changes: 48 additions & 0 deletions
48
...s-onchain-data/access-nodes/websockets-stream-api/list-subscriptions-message.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: List subscriptions request message format | ||
sidebar_label: Listing subscriptions | ||
sidebar_position: 3 | ||
--- | ||
|
||
# List subscriptions message format | ||
|
||
List subscriptions requests must be sent as JSON in text frames, one request per frame. | ||
This message is different from other as it doesn't require you to provide subscription ID. | ||
Thus, the response for this message is different too. | ||
|
||
### Example of request | ||
|
||
```json | ||
{ | ||
"action": "list_subscriptions" | ||
} | ||
``` | ||
|
||
### Example of response | ||
|
||
```json | ||
{ | ||
"subscriptions": [ | ||
{ | ||
"subscription_id": "uuid-1", | ||
"topic": "blocks", | ||
"parameters": { | ||
"start_block_height": "123456789" | ||
} | ||
}, | ||
{ | ||
"subscription_id": "uuid-2", | ||
"topic": "events", | ||
"parameters": {} | ||
} | ||
] | ||
} | ||
``` | ||
|
||
If there are no active subscriptions, `subscriptions` array will be empty. | ||
|
||
### Request fields | ||
|
||
| Name | Type | Mandatory | Description | | ||
|----------|--------|-----------|-----------------------------------------------------------------------------------------| | ||
| `action` | STRING | YES | Action to perform. Must be `list_subscriptions` to initiate a list subscription request | |
81 changes: 81 additions & 0 deletions
81
...ops/access-onchain-data/access-nodes/websockets-stream-api/subscribe-message.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
--- | ||
title: Subscribe request message format | ||
sidebar_label: Subscribing to topic | ||
sidebar_position: 1 | ||
--- | ||
|
||
# Subscribe request format | ||
|
||
Subscribe requests must be sent as JSON in text frames, one request per frame. | ||
|
||
### Example of subscribe request | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786", | ||
"action": "subscribe", | ||
"topic": "block_digests", | ||
"parameters": { | ||
"start_block_height": "99,416,580" | ||
} | ||
} | ||
``` | ||
|
||
### Example of successful response | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786" | ||
} | ||
``` | ||
|
||
### Example of failed response | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786", | ||
"error": { | ||
"code": 400, | ||
"message": "invalid message" | ||
} | ||
} | ||
``` | ||
|
||
### Example of messages provided by subscription (if successful) | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786", | ||
"payload": { | ||
"id": "0x1234...", | ||
"height:": "123456789", | ||
"timestamp": "2025-01-02T10:00:00Z" | ||
} | ||
} | ||
``` | ||
|
||
### Example of messages provided by subscription (if error) | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786", | ||
"error": { | ||
"code": 500, | ||
"message": "access node is not responsible" | ||
} | ||
} | ||
``` | ||
|
||
### Request fields: | ||
|
||
| Name | Type | Mandatory | Description | | ||
|-------------------|--------|-----------|--------------------------------------------------------------------------------------------| | ||
| `subscription_id` | UUID | NO | Optional unique identifier for the subscription. Server will generate one if omitted | | ||
| `action` | STRING | YES | Action to perform. Must be `subscribe` to initiate a subscription | | ||
| `topic` | STRING | YES | The topic to subscribe to, such as `blocks`, `block_digests`, etc. | | ||
| `parameters` | STRING | NO | Additional parameters for the subscription, such as `start_block_id`, `start_block_height` | | ||
|
||
You can use `subscription_id` as a client-generated identifier to track responses asynchronously. | ||
If you don't provide `subscription_id`, the server will generate one and include it in the response. | ||
|
||
The order of params is not significant. |
44 changes: 44 additions & 0 deletions
44
...s/access-onchain-data/access-nodes/websockets-stream-api/unsubscribe-message.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
title: Unsubscribe request message format | ||
sidebar_label: Unsubscribing from topic | ||
sidebar_position: 2 | ||
--- | ||
|
||
# Unsubscribe message format | ||
|
||
Unsubscribe requests must be sent as JSON in text frames, one request per frame. | ||
|
||
### Example of unsubscribe request | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786", | ||
"action": "unsubscribe" | ||
} | ||
``` | ||
|
||
### Example of successful response | ||
|
||
```json | ||
{ | ||
"subscription_id": "fa770c92-a1f1-4375-9a7b-e13d9aac0786" | ||
} | ||
``` | ||
|
||
### Example of error response | ||
|
||
```json | ||
{ | ||
"error": { | ||
"code": 404, | ||
"message": "subscription not found" | ||
} | ||
} | ||
``` | ||
|
||
### Request fields | ||
|
||
| Name | Type | Mandatory | Description | | ||
|-------------------|--------|-----------|-----------------------------------------------------------------------| | ||
| `subscription_id` | UUID | YES | Unique identifier of the subscription | | ||
| `action` | STRING | YES | Action to perform. Must be `unsubscribe` to initiate a unsubscription | |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.