Using WebSockets with GraphQL
⚠️ Important: WebSocket support is available but should be used at your own risk on plans other than dedicated.
Overview
By default, HyperIndex provides GraphQL endpoints over HTTP/HTTPS. However, you can also connect to your GraphQL endpoint using WebSocket connections (WSS/WS) for real-time subscriptions and persistent connections.
WebSocket Support Status
WebSocket connections are available with different levels of support depending on your hosting plan:
- Dedicated: WebSockets are fully supported. The dedicated package includes additional infrastructure such as connection pooling which enables proper WebSocket support.
- Other Plans: WebSocket support is available but should be used at your own risk. These plans do not have the same infrastructure support for WebSockets as dedicated. We don't recommend relying on WebSockets for more than 10 concurrent connections.
How to Use WebSockets
To use WebSockets with your HyperIndex GraphQL endpoint, simply swap the protocol in your endpoint URL:
Protocol Mapping
- HTTPS → WSS: Change
https://towss://in your GraphQL endpoint URL - HTTP → WS: Change
http://tows://in your GraphQL endpoint URL
Example
If your GraphQL endpoint is:
https://indexer.hyperindex.xyz/123abcd/v1/graphql
You can connect via WebSocket using:
wss://indexer.hyperindex.xyz/123abcd/v1/graphql
Similarly, for HTTP endpoints:
http://localhost:8080/v1/graphql
Becomes:
ws://localhost:8080/v1/graphql
Authentication
Browsers can't set headers on a WebSocket handshake, so graphql-ws sends them in connectionParams when the connection opens. Hasura reads them from connectionParams.headers. For example, to use the local admin role (which can also subscribe to _aggregate fields):
const client = createClient({
url: 'ws://localhost:8080/v1/graphql',
connectionParams: { headers: { 'x-hasura-admin-secret': 'testing' } },
});
Don't put the admin secret in frontend code. If your Envio Cloud endpoint uses API key authentication, contact the Envio team to confirm how to authenticate WebSocket connections.
Usage Example
Here's a TypeScript example using the graphql-ws library to subscribe to real-time updates from your indexer:
import { createClient } from 'graphql-ws';
// Define the entity you are subscribing to (must match your schema definition)
interface Swap {
id: string;
}
// The shape of `data` in each result: one key per root field
interface SubscriptionData {
Swap: Swap[];
}
const client = createClient({
url: 'ws://localhost:8080/v1/graphql',
});
console.log('Connecting to WebSocket...');
client.subscribe<SubscriptionData>(
{
query: `
subscription {
Swap(order_by: { id: desc }, limit: 10) {
id
}
}
`,
},
{
next: (result) => {
const swaps = result.data?.Swap ?? [];
console.log('\n📥 Latest swaps:');
console.log(JSON.stringify(swaps, null, 2));
},
error: (err) => {
console.error('❌ Error:', err);
},
complete: () => {
console.log('✅ Subscription completed');
},
}
);
Installation
To use this example, install graphql-ws:
npm install graphql-ws
# or
yarn add graphql-ws
Browsers and Node.js 22+ have a global WebSocket. On older Node.js versions, also install ws and pass it to the client: import WebSocket from 'ws'; and createClient({ url, webSocketImpl: WebSocket }).
Live Queries and Streaming Subscriptions
A subscription on a plain root field, such as Swap(...) above, is a live query: every message carries the whole current result (here the latest 10 rows), not just the new rows. Order by a field that follows chain order, such as a block number or timestamp, because string ids don't sort chronologically.
To receive only new rows, use the <Entity>_stream root field with a cursor. Each batch returns up to batch_size rows whose cursor value is greater than the last one sent, so the cursor field must be unique per row and increase with each insert. A shared value such as blockNumber skips rows: the rest of a block that has more than batch_size rows, and rows written later for a block the stream has already passed. For event rows on a single chain, store a sequence number in the handler, such as seq: BigInt! set to BigInt(event.block.number) * 1_000_000n + BigInt(event.logIndex):
subscription {
Transfer_stream(
batch_size: 50
cursor: { initial_value: { seq: 0 }, ordering: ASC }
) {
id
seq
value
}
}
The stream doesn't resend rows that are later updated, including rows rewritten after a reorg. Use a live query when you need those.
Getting Support
For any questions about WebSocket usage, contact the Envio team via Telegram or Discord.