A simple JavaScript project demonstrating how to use WebSocket as a client to interact with the Flow blockchain. This repository contains examples to subscribe to various Flow topics (such as block digests, block headers, blocks, events, account statuses, and transaction statuses) using WebSocket connections.
This example project is designed to test and demonstrate the functionality of WebSocket connections with the Flow blockchain. It shows how to subscribe to and handle real-time updates using different topics available on the Flow network. You can try out different scripts to see how subscriptions behave when provided with various arguments (including intentionally wrong ones).
- WebSocket Client Implementation: Connects to a configurable WebSocket endpoint.
- Multiple Examples: Scripts for subscribing to:
- Block digests
- Block headers
- Blocks
- Events
- Account statuses
- Transaction statuses
- Environment Configuration: Easily set the WebSocket endpoint via an environment file.
- Event Handling: Demonstrates handling of incoming messages, subscription acknowledgements, errors, and connection closures.
- Node.js (v14 or higher is recommended)
- npm (comes with Node.js)
-
Clone the repository:
git clone https://github.com/The-K-R-O-K/flow-stream-api-example.git cd flow-stream-api-example
-
Install dependencies:
npm install
The project uses an environment variable to configure the WebSocket endpoint. An example file is provided:
-
Copy the example environment file to create your own configuration:
cp example.env .env
-
Open the newly created
.env
file and update theWEBSOCKET_URL
with the correct endpoint. For example:WEBSOCKET_URL=wss://rest-mainnet.onflow.org/ws FLOW_ACCESS_NODE_API=https://rest-mainnet.onflow.org
The package.json
defines several npm scripts for different functionalities. You can run any of them directly. For example:
-
Block Digests from Start Height:
npm run block_digests:from_start_height
-
Block Headers from Start ID:
npm run block_headers:from_start_id
-
Blocks from Latest Block:
npm run blocks:from_latest_block
-
Events (and other topics):
There are similar scripts for events, account statuses, and transaction statuses. Use the command:npm run <script_name>
Replace
<script_name>
with one of the following:block_digests:from_start_id
block_digests:from_latest_block
block_digests:wrong_args
block_headers:from_start_height
block_headers:from_latest_block
block_headers:wrong_args
blocks:from_start_height
blocks:from_start_id
blocks:wrong_args
events:from_start_height
events:from_start_id
events:from_latest_block
events:wrong_args
account_statuses:from_start_height
account_statuses:from_start_id
account_statuses:from_latest_block
account_statuses:wrong_args
transaction_statuses:listen_tx_statuses
The main entry point for the generic WebSocket client is located at src/websocket_client.js
. This file demonstrates how to:
- Load configuration from the
.env
file. - Establish a WebSocket connection.
- Handle events such as
open
,message
,error
, andclose
.
To run the client directly, use:
node src/websocket_client.js
Upon connection, the client logs the WebSocket URL and confirms the connection. You can modify this file to send custom messages using the provided sendMessage()
method.
Contributions to this project are welcome. If you have ideas for improvements or additional examples, please fork the repository and submit a pull request.