WebSocket API
The Avail Light Client WebSocket API allows real-time communication between a client and a server over a persistent connection. The Web socket API can be used on its own or in combination with HTTP API to enable different pull/push use cases.
HTTP
and Websocket
are twi different ways of communicating with a server. HTTP
is a request-response protocol,
where a connection persists for only so long as the request is being processed. In contrast, a websocket connection
persists for as long as the client and server are connected. This allows for real-time communication between the two,
and enables the client to fetch information from the server as soon as it is available, without having to
poll the server for updates.
HOW TO CONNECT TO THE WEBSOCKET SERVER
You cannot use CURL
to send websocket commands, since CURL
cannot maintain a persistent connection.
Although there are a variety of librariries we can use, the following examples are using wscat (opens in a new tab).
To install wscat
, simply run:
npm install -g wscat
Get started with the Websocket Avail LC API
The Avail LC Websocket API generates a unique subscription_id
upon request. This ID is used to connect
to the websocket server.
The v2/subscriptions
method creates subscriptions for given topics. In case of reconnects, the user needs to subscribe again.
Topics
- header-verified - header finality is verified and header is available
- confidence-achieved - confidence is achieved
- data-verified - block data is verified and available
Data fields
Filters data-verified message. Optional parameter used when encoded extrinsic is needed. If omitted, only decoded data is present in the message.
Request:
curl -X POST "https://api.lightclient.mainnet.avail.so/v2/subscriptions" \
-H "Content-Type: application/json" \
-d '{
"topics": ["header-verified", "confidence-achieved", "data-verified"],
"data_fields": ["data", "extrinsic"]
}'
Sample Response:
{
"subscription_id": "{subscription-id}"
}