Skip to content

Commit bd5a174

Browse files
committed
ha mode polish
1 parent 20be00e commit bd5a174

File tree

3 files changed

+90
-103
lines changed

3 files changed

+90
-103
lines changed

src/content/data-streams/tutorials/go-sdk-stream.mdx

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -327,53 +327,49 @@ The decoded report details include:
327327

328328
For descriptions and data types of other report schemas, see the [Report Schema Overview](/data-streams/reference/report-schema-overview).
329329

330+
### Subscribing to multiple streams
331+
332+
You can subscribe to multiple streams by providing additional stream IDs as command-line arguments:
333+
334+
```bash
335+
go run stream.go 0x000359843a543ee2fe414dc14c7e7920ef10f4372990b79d6361cdc0dd1ba782 0x00036fe43f87884450b4c7e093cd5ed99cac6640d8c2000e6afc02c8838d0265
336+
```
337+
338+
This will subscribe to both ETH/USD and BTC/USD streams.
339+
330340
### High Availability (HA) mode
331341

332342
<Aside type="note" title="Mainnet only">
333343
HA mode is only available on mainnet endpoints, not testnet.
334344
</Aside>
335345

336-
High Availability (HA) mode creates multiple WebSocket connections to different origin endpoints for improved reliability. When enabled, the SDK automatically handles failover, deduplicates reports, and provides connection-level metrics.
346+
The example above demonstrates streaming data from a single crypto stream. For production environments, especially when subscribing to multiple streams, it's recommended to enable [High Availability (HA) mode](/data-streams/reference/data-streams-api/go-sdk#high-availability-ha-mode).
347+
348+
High Availability (HA) mode creates multiple WebSocket connections to different origin endpoints for improved reliability. When HA mode is enabled, the Stream will maintain at least 2 concurrent connections to different instances to ensure high availability, fault tolerance and minimize the risk of report gaps.
337349

338350
#### Enabling HA mode
339351

340352
To enable HA mode in your streaming application, make these changes to the basic example:
341353

342-
1. Add HA mode to the client configuration and use a mainnet endpoint, as HA mode is not currently supported on testnet:
343-
344354
```go
355+
// ... existing code ...
356+
357+
// Enable HA mode with mainnet endpoint
345358
cfg := streams.Config{
346359
ApiKey: os.Getenv("API_KEY"),
347360
ApiSecret: os.Getenv("API_SECRET"),
348361
WsURL: "wss://ws.dataengine.chain.link", // Use mainnet endpoint for HA mode
349362
WsHA: true, // Enable High Availability mode
350363
Logger: streams.LogPrintf,
351364
}
352-
```
353-
354-
2. Monitor HA metrics using the existing `Stats()` calls in your report handler:
355-
356-
```go
357-
// In your report processing loop
358-
for {
359-
reportResponse, err := stream.Read(context.Background())
360-
if err != nil {
361-
cfg.Logger("Error reading from stream: %v\n", err)
362-
continue
363-
}
364-
365-
// Process the report...
366365

367-
// Monitor HA statistics (includes HA-specific metrics)
368-
cfg.Logger("Stream Stats: %s\n", stream.Stats().String())
369-
// Output example: "accepted: 150, deduplicated: 3, total_received: 153, partial_reconnects: 1, full_reconnects: 0, configured_connections: 2, active_connections: 2"
366+
client, err := streams.New(cfg)
367+
if err != nil {
368+
cfg.Logger("Failed to create client: %v\n", err)
369+
os.Exit(1)
370370
}
371-
```
372371

373-
3. Optionally, monitor individual connection status using `StreamWithStatusCallback`:
374-
375-
```go
376-
// Subscribe to the feeds with connection status monitoring
372+
// Optional: Use StreamWithStatusCallback for connection monitoring
377373
stream, err := client.StreamWithStatusCallback(
378374
ctx, ids,
379375
func(isConnected bool, host string, origin string) {
@@ -384,21 +380,15 @@ stream, err := client.StreamWithStatusCallback(
384380
cfg.Logger("Host: %s, Origin: %s, Status: %s\n", host, origin, status)
385381
},
386382
)
387-
```
388-
389-
When HA mode is enabled, the SDK maintains at least 2 concurrent connections to different instances to ensure high availability, fault tolerance, and minimize the risk of report gaps.
390-
391-
See more details about HA mode in the [SDK Reference](/data-streams/reference/data-streams-api/go-sdk#high-availability-ha-mode).
392383

393-
### Subscribing to multiple streams
384+
// ... existing code ...
385+
```
394386

395-
You can subscribe to multiple streams by providing additional stream IDs as command-line arguments:
387+
When `WsHA` is `true`, the SDK automatically discovers multiple origin endpoints behind the single URL and establishes separate connections to each origin. You also must use a mainnet endpoint, as HA mode is not currently supported on testnet.
396388

397-
```bash
398-
go run stream.go 0x000359843a543ee2fe414dc14c7e7920ef10f4372990b79d6361cdc0dd1ba782 0x00036fe43f87884450b4c7e093cd5ed99cac6640d8c2000e6afc02c8838d0265
399-
```
389+
The optional `StreamWithStatusCallback` can be used to monitor individual connection status. The SDK already provides comprehensive connection logs through `stream.Stats().String()`, so this callback is primarily useful for custom alerting or connection monitoring.
400390

401-
This will subscribe to both ETH/USD and BTC/USD streams.
391+
See more details about HA mode in the [SDK Reference](/data-streams/reference/data-streams-api/go-sdk#high-availability-ha-mode).
402392

403393
### Payload for onchain verification
404394

src/content/data-streams/tutorials/rust-sdk-stream.mdx

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -310,25 +310,6 @@ In this tutorial, you'll learn how to use the [Data Streams SDK](/data-streams/r
310310

311311
[Learn more about the decoded report details](#decoded-report-details).
312312

313-
### High Availability (HA) Mode
314-
315-
The example above demonstrates streaming data from a single crypto stream. For production environments, especially when subscribing to multiple streams, it's recommended to enable [High Availability (HA) mode](https://github.com/smartcontractkit/data-streams-sdk/blob/main/rust/docs/examples/wss_multiple.md). This can be achieved by:
316-
317-
1. Adding multiple WebSocket endpoints in the configuration:
318-
319-
```rust
320-
"wss://ws.testnet-dataengine.chain.link,wss://ws.testnet-dataengine.chain.link"
321-
```
322-
323-
1. Enabling HA mode in the configuration:
324-
```rust
325-
use chainlink_data_streams_sdk::config::WebSocketHighAvailability;
326-
// ...
327-
.with_ws_ha(WebSocketHighAvailability::Enabled)
328-
```
329-
330-
When HA mode is enabled and multiple WebSocket origins are provided, the Stream will maintain concurrent connections to different instances. This ensures high availability, fault tolerance, and minimizes the risk of report gaps.
331-
332313
#### Subscribing to multiple streams
333314

334315
You can subscribe to multiple streams by providing additional stream IDs as command-line arguments:
@@ -341,6 +322,41 @@ cargo run -- \
341322

342323
This will subscribe to both ETH/USD and BTC/USD streams.
343324

325+
### High Availability (HA) mode
326+
327+
<Aside type="note" title="Mainnet only">
328+
HA mode is only available on mainnet endpoints, not testnet.
329+
</Aside>
330+
331+
The example above demonstrates streaming data from a single crypto stream. For production environments, especially when subscribing to multiple streams, it's recommended to enable [High Availability (HA) mode](/data-streams/reference/data-streams-api/rust-sdk#high-availability-mode).
332+
333+
High Availability (HA) mode creates multiple WebSocket connections to different origin endpoints for improved reliability. When HA mode is enabled, the Stream will maintain at least 2 concurrent connections to different instances to ensure high availability, fault tolerance and minimize the risk of report gaps.
334+
335+
#### Enabling HA mode
336+
337+
To enable HA mode in your streaming application, make these changes to the basic example. You also must use a mainnet endpoint, as HA mode is not currently supported on testnet.
338+
339+
```rust
340+
// ... existing code ...
341+
342+
use chainlink_data_streams_sdk::config::{Config, WebSocketHighAvailability}; // Import WebSocketHighAvailability
343+
344+
// Initialize the configuration with HA mode
345+
let config = Config::new(
346+
api_key,
347+
api_secret,
348+
"https://api.dataengine.chain.link".to_string(), // Mainnet endpoint
349+
"wss://ws.dataengine.chain.link,wss://ws.dataengine.chain.link".to_string(), // Multiple WebSocket endpoints
350+
)
351+
.with_ws_ha(WebSocketHighAvailability::Enabled) // Enable WebSocket High Availability Mode
352+
.build()?;
353+
354+
355+
// ... existing code ...
356+
```
357+
358+
See more details about HA mode in the [SDK Reference](/data-streams/reference/data-streams-api/rust-sdk#high-availability-mode).
359+
344360
### Decoded report details
345361

346362
The decoded report details include:
@@ -359,14 +375,6 @@ The decoded report details include:
359375

360376
For descriptions and data types of other report schemas, see the [Report Schema Overview](/data-streams/reference/report-schema-overview).
361377

362-
### High Availability (HA) mode
363-
364-
<Aside type="note" title="Mainnet only">
365-
HA mode is only available on mainnet endpoints, not testnet.
366-
</Aside>
367-
368-
High Availability (HA) mode creates multiple WebSocket connections to different origin endpoints for improved reliability. When enabled, the SDK automatically handles failover, deduplicates reports, and provides connection-level metrics.
369-
370378
### Payload for onchain verification
371379

372380
In this tutorial, you logged and decoded the `full_report` payloads to extract the report data. However, in a production environment, you should verify the data to ensure its integrity and authenticity.

src/content/data-streams/tutorials/ts-sdk-stream.mdx

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -346,77 +346,66 @@ The decoded report details include:
346346
347347
For descriptions and data types of other report schemas, see the [Report Schema Overview](/data-streams/reference/report-schema-overview).
348348
349+
### Subscribing to multiple streams
350+
351+
You can subscribe to multiple streams by providing additional stream IDs as command-line arguments:
352+
353+
```bash
354+
npx tsx stream.ts 0x000359843a543ee2fe414dc14c7e7920ef10f4372990b79d6361cdc0dd1ba782,0x00036fe43f87884450b4c7e093cd5ed99cac6640d8c2000e6afc02c8838d0265
355+
```
356+
357+
This will subscribe to both ETH/USD and BTC/USD streams.
358+
349359
### High Availability (HA) mode
350360
351361
<Aside type="note" title="Mainnet only">
352362
HA mode is only available on mainnet endpoints, not testnet.
353363
</Aside>
354364
355-
High Availability (HA) mode creates multiple WebSocket connections to different origin endpoints for improved reliability. When enabled, the SDK automatically handles failover, deduplicates reports, and provides connection-level metrics.
365+
The example above demonstrates streaming data from a single crypto stream. For production environments, especially when subscribing to multiple streams, it's recommended to enable [High Availability (HA) mode](/data-streams/reference/data-streams-api/ts-sdk#high-availability-mode).
366+
367+
High Availability (HA) mode creates multiple WebSocket connections to different origin endpoints for improved reliability. When HA mode is enabled, the Stream will maintain at least 2 concurrent connections to different instances to ensure high availability, fault tolerance and minimize the risk of report gaps.
356368
357369
#### Enabling HA mode
358370
359371
To enable HA mode in your streaming application, make these changes to the basic example:
360372
361-
1. Add `haMode: true` to the client configuration. You also must use a mainnet endpoint, as HA mode is not currently supported on testnet.
362-
363373
```typescript
374+
// ... existing code ...
375+
364376
const client = createClient({
365377
apiKey: process.env.API_KEY || "YOUR_API_KEY",
366378
userSecret: process.env.USER_SECRET || "YOUR_USER_SECRET",
367379
endpoint: "https://api.dataengine.chain.link", // Mainnet endpoint
368-
wsEndpoint: "wss://ws.dataengine.chain.link", // Mainnet WebSocket
380+
wsEndpoint: "wss://ws.dataengine.chain.link", // Single endpoint (mainnet only)
369381
haMode: true, // Enable High Availability mode
370382
371-
// Optional: Monitor connection status changes
372-
connectionStatusCallback: (isConnected: boolean, host: string, origin?: string) => {
373-
const status = isConnected ? "🟢 Connected" : "🔴 Disconnected"
374-
console.log(`${status}: ${host}${origin ? ` (${origin})` : ""}`)
383+
// Optional: Advanced connection monitoring with origin tracking
384+
connectionStatusCallback: (isConnected, host, origin) => {
385+
const timestamp = new Date().toISOString().substring(11, 19)
386+
const status = isConnected ? "🟢 UP" : "🔴 DOWN"
387+
console.log(`[${timestamp}] ${status} ${host}${origin || ""}`)
388+
389+
// Example: Send alerts for specific origins
390+
if (!isConnected && origin) {
391+
console.warn(`⚠️ Alert: Origin ${origin} on ${host} went offline`)
392+
}
375393
},
376394
377395
logging: {
378396
logger: console,
379397
logLevel: LogLevel.INFO,
380398
},
381399
})
382-
```
383-
384-
2. Optionally, monitor HA metrics using the existing `getMetrics()` calls in your report handler:
385400
386-
```typescript
387-
stream.on("report", (report) => {
388-
// ... your report processing logic
389-
390-
// HA-specific metrics
391-
const stats = stream.getMetrics()
392-
console.log(
393-
`📊 HA Stats: ${stats.accepted} reports | ${stats.activeConnections}/${stats.configuredConnections} connections`
394-
)
395-
console.log(`Reconnects: ${stats.partialReconnects} partial, ${stats.fullReconnects} full`)
396-
397-
// Check deduplication (important for HA mode)
398-
if (stats.totalReceived > 0) {
399-
const deduplicationRate = ((stats.deduplicated / stats.totalReceived) * 100).toFixed(1)
400-
console.log(`📈 Deduplication: ${deduplicationRate}% (${stats.deduplicated} filtered)`)
401-
}
402-
})
401+
// ... existing code ...
403402
```
404403
405-
When `haMode` is `true`, the SDK automatically discovers multiple origin endpoints behind the single URL and establishes separate connections to each origin.
404+
When `haMode` is `true`, the SDK automatically discovers multiple origin endpoints behind the single URL and establishes separate connections to each origin. You also must use a mainnet endpoint, as HA mode is not currently supported on testnet.
406405
407406
The optional `connectionStatusCallback` can be used to integrate with external monitoring systems. The SDK already provides comprehensive connection logs, so this callback is primarily useful for custom alerting or metrics collection.
408407
409-
See more details about HA mode in the [SDK Reference](/ts-sdk#high-availability-mode).
410-
411-
### Subscribing to multiple streams
412-
413-
You can subscribe to multiple streams by providing additional stream IDs as command-line arguments:
414-
415-
```bash
416-
npx tsx stream.ts 0x000359843a543ee2fe414dc14c7e7920ef10f4372990b79d6361cdc0dd1ba782,0x00036fe43f87884450b4c7e093cd5ed99cac6640d8c2000e6afc02c8838d0265
417-
```
418-
419-
This will subscribe to both ETH/USD and BTC/USD streams.
408+
See more details about HA mode in the [SDK Reference](/data-streams/reference/data-streams-api/ts-sdk#high-availability-ha-mode).
420409
421410
### Payload for onchain verification
422411

0 commit comments

Comments
 (0)