plugins/midnight-indexer/skills/event-subscriptions/SKILL.md
Use when subscribing to real-time blockchain events, setting up WebSocket connections, monitoring contract state changes, building live dashboards, or implementing push notifications from Midnight.
npx skillsauth add aaronbassett/midnight-knowledgebase midnight-indexer:event-subscriptionsInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Subscribe to real-time blockchain events from the Midnight Network using WebSocket connections.
The Midnight indexer provides GraphQL subscriptions over WebSocket for real-time event delivery.
| Component | Purpose | |-----------|---------| | WebSocket Connection | Persistent bidirectional channel | | GraphQL Subscriptions | Event-driven queries | | Subscription Filters | Target specific events |
| Event | Description |
|-------|-------------|
| newTransaction | Transaction confirmed in a block |
| newBlock | New block added to chain |
| contractStateChange | Contract state updated |
| utxoCreated | New UTXO created for address |
| utxoSpent | UTXO spent by transaction |
When events arrive faster than your application can process them, implement backpressure strategies:
| Document | Description | |----------|-------------| | websocket-setup.md | Connection configuration and protocols | | reconnection-patterns.md | Handling disconnections and replay |
| Example | Description | |---------|-------------| | contract-events/ | Subscribe to contract state changes | | reconnect-handler/ | Robust reconnection with event replay |
import { createWebSocketClient } from '@midnight-ntwrk/midnight-js-indexer';
const wsClient = createWebSocketClient({
uri: 'wss://indexer.testnet.midnight.network/api/v1/graphql',
});
const subscription = wsClient.subscribe({
query: `
subscription WatchTransactions($address: String!) {
newTransaction(address: $address) {
hash
blockNumber
timestamp
inputs { address amount }
outputs { address amount }
}
}
`,
variables: { address: 'addr_test1...' },
});
subscription.on('data', (event) => {
console.log('New transaction:', event.data.newTransaction);
});
subscription.on('data', handleTransaction);
subscription.on('error', handleError);
subscription.on('complete', handleComplete);
// When done listening
subscription.unsubscribe();
wsClient.close();
interface SubscriptionHandlers<T> {
onData: (data: T) => void;
onError: (error: Error) => void;
onComplete?: () => void;
}
function createEventSubscription<T>(
client: WebSocketClient,
query: string,
variables: Record<string, unknown>,
handlers: SubscriptionHandlers<T>
): () => void {
const subscription = client.subscribe({ query, variables });
subscription.on('data', (result) => {
if (result.errors) {
handlers.onError(new Error(result.errors[0].message));
return;
}
handlers.onData(result.data);
});
subscription.on('error', handlers.onError);
if (handlers.onComplete) {
subscription.on('complete', handlers.onComplete);
}
// Return unsubscribe function
return () => subscription.unsubscribe();
}
interface ContractEvent {
contractAddress: string;
key: string;
oldValue: string | null;
newValue: string;
txHash: string;
blockNumber: number;
}
function watchContractState(
client: WebSocketClient,
contractAddress: string,
onEvent: (event: ContractEvent) => void
): () => void {
return createEventSubscription(
client,
`
subscription WatchContract($address: String!) {
contractStateChange(address: $address) {
contractAddress
key
oldValue
newValue
txHash
blockNumber
}
}
`,
{ address: contractAddress },
{
onData: (data) => onEvent(data.contractStateChange),
onError: (error) => console.error('Subscription error:', error),
}
);
}
async function waitForConfirmation(
client: WebSocketClient,
txHash: string,
confirmations = 1,
timeout = 120000
): Promise<number> {
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
unsubscribe();
reject(new Error('Confirmation timeout'));
}, timeout);
const unsubscribe = createEventSubscription(
client,
`
subscription TrackTx($hash: String!) {
transactionConfirmation(hash: $hash) {
hash
blockNumber
confirmations
}
}
`,
{ hash: txHash },
{
onData: (data) => {
const { confirmations: current } = data.transactionConfirmation;
if (current >= confirmations) {
clearTimeout(timer);
unsubscribe();
resolve(data.transactionConfirmation.blockNumber);
}
},
onError: (error) => {
clearTimeout(timer);
reject(error);
},
}
);
});
}
class SubscriptionManager {
private subscriptions = new Map<string, () => void>();
private client: WebSocketClient;
constructor(wsUri: string) {
this.client = createWebSocketClient({ uri: wsUri });
}
subscribe(
id: string,
query: string,
variables: Record<string, unknown>,
onData: (data: unknown) => void
): void {
// Unsubscribe existing with same ID
this.unsubscribe(id);
const unsub = createEventSubscription(
this.client,
query,
variables,
{
onData,
onError: (err) => console.error(`Subscription ${id} error:`, err),
}
);
this.subscriptions.set(id, unsub);
}
unsubscribe(id: string): void {
const unsub = this.subscriptions.get(id);
if (unsub) {
unsub();
this.subscriptions.delete(id);
}
}
unsubscribeAll(): void {
for (const [id] of this.subscriptions) {
this.unsubscribe(id);
}
}
close(): void {
this.unsubscribeAll();
this.client.close();
}
}
class EventBuffer<T> {
private buffer: T[] = [];
private processing = false;
private intervalHandle: ReturnType<typeof setInterval>;
constructor(
private processor: (events: T[]) => Promise<void>,
private maxSize = 100,
private flushInterval = 1000
) {
this.intervalHandle = setInterval(() => this.flush(), flushInterval);
}
destroy(): void {
clearInterval(this.intervalHandle);
}
push(event: T): void {
this.buffer.push(event);
if (this.buffer.length >= this.maxSize) {
this.flush();
}
}
async flush(): Promise<void> {
if (this.processing || this.buffer.length === 0) return;
this.processing = true;
const events = this.buffer.splice(0);
try {
await this.processor(events);
} catch (error) {
console.error('Buffer processing error:', error);
// Re-queue failed events at front
this.buffer.unshift(...events);
} finally {
this.processing = false;
}
}
}
indexer-service - Query historical blockchain datamidnight-dapp:state-management - Sync frontend state with eventsmidnight-dapp:transaction-flows - Submit transactions and track status/midnight-tooling:check - Verify WebSocket connectivitytools
Use when setting up Midnight development environment, installing Compact compiler and developer tools, configuring proof server, verifying prerequisites, or getting started with Midnight development.
tools
--- name: midnight-tooling:midnight-debugging description: Use when encountering Midnight errors like "compact: command not found", "ERR_UNSUPPORTED_DIR_IMPORT", version mismatches, proof server failures, "@midnight-ntwrk" package errors, or compilation failures. --- # Midnight Environment Debugging Expert knowledge for identifying and resolving common Midnight development toolchain issues. ## Diagnostic Approach When encountering Midnight-related errors, follow this systematic approach: 1.
tools
Use when checking Midnight version compatibility, understanding pragma language_version, verifying compiler and runtime version relationships, or troubleshooting version mismatch errors between Midnight components.
tools
Use when setting up CI/CD for Midnight projects, configuring GitHub Actions for Compact contract compilation, running TypeScript tests in CI, validating version consistency, or automating contract builds.