Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,20 @@ const path = Zenon.getPowBasePath();

### Instance Methods

##### `initialize(url: string): Promise<void>`
##### `initialize(url: string, timeout?: number, wsOptions?: WsClientOptions): Promise<void>`

Connect to a Zenon node via HTTP or WebSocket.

```javascript
// WebSocket (for subscriptions)
// WebSocket (for subscriptions and transactions)
await zenon.initialize('wss://node.zenonhub.io:35998');

// HTTP (for simple requests)
await zenon.initialize('https://node.zenonhub.io:35997');
```

> **Note:** WebSocket connections automatically reconnect if dropped during long-running operations (e.g., PoW generation). The default settings are suitable for most use cases.

##### `clearConnection(): void`

Disconnect and clean up resources.
Expand Down
32 changes: 30 additions & 2 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,36 @@ await zenon.initialize('wss://node.zenonhub.io:35998');
```

Connection options:
- **HTTP**: `https://node.zenonhub.io:35997`
- **WebSocket**: `wss://node.zenonhub.io:35998`
- **HTTP**: `https://node.zenonhub.io:35997` - For simple API calls
- **WebSocket**: `wss://node.zenonhub.io:35998` - For real-time subscriptions and transactions

### WebSocket Configuration

For long-running operations (like PoW generation), you can configure WebSocket reconnection behavior:

```javascript
import { Zenon } from 'znn-typescript-sdk';
import { WsClientOptions } from 'znn-typescript-sdk';

const zenon = Zenon.getInstance();

// Use defaults (auto-reconnect enabled)
await zenon.initialize('wss://node.zenonhub.io:35998');

// Custom timeout (in milliseconds)
await zenon.initialize('wss://node.zenonhub.io:35998', 60000);

// Custom reconnection settings
const wsOptions: WsClientOptions = {
reconnect: true, // Enable auto-reconnect
reconnect_interval: 1000, // 1 second between attempts
max_reconnects: 0 // 0 = unlimited attempts
};

await zenon.initialize('wss://node.zenonhub.io:35998', 30000, wsOptions);
```

**Note:** WebSocket connections automatically reconnect if dropped during operations. The default settings work well for most cases, including PoW generation.

---

Expand Down
2 changes: 1 addition & 1 deletion src/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type { Client } from "./interfaces.js"
export { WSUpdateStream, WsClient } from "./websocket.js"
export { WSUpdateStream, WsClient, type WsClientOptions } from "./websocket.js"
export { HttpClient } from "./http.js"
export { newClient } from "./factory.js"
export { ZnnClientException } from "./errors.js"
25 changes: 21 additions & 4 deletions src/client/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ enum WebsocketStatus {
Stopped,
}

export interface WsClientOptions {
autoconnect?: boolean;
reconnect?: boolean;
reconnect_interval?: number;
max_reconnects?: number;
}

type WSSubscriptionCallback = (data: any[]) => void;

class WSSubscriptions {
Expand Down Expand Up @@ -69,11 +76,21 @@ export class WsClient implements ClientInterface {
this.subscriptions = new WSSubscriptions();
}

initialize(url: string, retry = true, timeout = 30000): Promise<void> {
initialize(url: string, timeout = 30000, options?: WsClientOptions): Promise<void> {
return new Promise<void>(async (resolve, reject) => {
try {
this.url = url;
this._wsRpc2Client = new webSocket(this.url, {autoconnect: true, reconnect: retry});

// Merge default options with user-provided options
const wsOptions = {
autoconnect: true,
reconnect: true,
reconnect_interval: 1000,
max_reconnects: 0, // 0 = unlimited reconnects
...options
};

this._wsRpc2Client = new webSocket(this.url, wsOptions);

logger.info(
`Initializing websocket connection to:${this.url} on chainIdentifier ${Zenon.getChainIdentifier()}`
Expand Down Expand Up @@ -108,13 +125,13 @@ export class WsClient implements ClientInterface {
return this._websocketIntendedState;
}

async restart(): Promise<void> {
async restart(options?: WsClientOptions): Promise<void> {
if (this._websocketIntendedState != WebsocketStatus.Running) {
return;
}
if (this._wsRpc2Client != null && this._wsRpc2Client!.isClosed == true) {
logger.info("Restarting websocket connection ...");
await this.initialize(this.url!, true);
await this.initialize(this.url!, 30000, options);
logger.info("Websocket connection successfully restarted");
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export {
DEFAULT_CHAIN_ID, DEFAULT_NET_ID, DEFAULT_POW_BASE_PATH
} from "./zenon.js";

//
// Client exports
export type { WsClientOptions } from "./client/index.js";

//
// ABI exports
export * from "./abi/index.js";
Expand Down
6 changes: 3 additions & 3 deletions src/zenon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Client, WsClient, newClient } from "./client/index.js";
import { Client, WsClient, newClient, type WsClientOptions } from "./client/index.js";
import { SubscribeApi, LedgerApi, StatsApi, EmbeddedApi } from "./api/index.js";
import { AccountBlockTemplate } from "./model/nom/accountBlock.js";
import { KeyPair } from "./wallet/index.js";
Expand Down Expand Up @@ -53,12 +53,12 @@ export class Zenon {
}
}

async initialize(serverUrl = this.defaultServerUrl, retry = true, timeout = 30000) {
async initialize(serverUrl = this.defaultServerUrl, timeout = 30000, wsOptions?: WsClientOptions) {
this.client = newClient(serverUrl);

// If it's a WebSocket client, initialize it
if (this.client instanceof WsClient) {
await this.client.initialize(serverUrl, retry, timeout);
await this.client.initialize(serverUrl, timeout, wsOptions);
}

this._setClient(this.client);
Expand Down
Loading