Connection Providers
Connection Providers provide an endpoint for interacting with the full node's RPC API. There are three connection providers on Vite: HTTP, WebSocket and IPC.
- HTTP: set up connection via http/https. The default port on the node is 48132.
- WebSocket: set up connection via ws/wss. The default port is 41420.
- IPC: set up connection via ipc sockets.
There are no obvious advantages and disadvantages between HTTP and WebSocket. However, if your program applies subscriptions, WebSocket is recommended as it updates in real time whereas HTTP polls for new data every few seconds. This chapter of Vite.js documentation will be brief as calling methods on these providers are best done by the ViteAPI class covered in the next chapter.
IPC provider is a good option when using a local node since it gives the most secure connection.
WebSocket
Installation
Note
Starting from 2.3.19, package @vite/vitejs-ws is consolidated into @vite/vitejs, so there's no need to install it separately.
Module Import
WS_RPC Class
-
Constructor
path: string = 'ws://localhost:41420'Connection URLtimeout: number = 60000Timeout in millisecondsoptions: objectprotocol: string = ''headers?: objectRequest headerclientConfig?: objectwebsocket client config optionsretryTimes: number = 10Times to retry connecting if it fails the first timeretryInterval: number = 10000Time between each retry attempt in milliseconds
-
Example
ReconnectHandler and Subscription Renewal
In 2.3.19, ReconnectHandler, AlwaysReconnect and RenewSubscription are added to support flexible reconnection mechanism and subscription renewal.
Breaking Change
retryTimes and retryInterval are moved from the options of provider to ReconnectHandler's constructor.
Let's see a few examples.
- Auto resubscribe with maximum 5 retries and 1000ms interval.
const ws = new WS_RPC(config.wsURL);
const provider = new ViteAPI(ws, () => {
console.log("Connected");
}, new RenewSubscription(retryTimes = 5, retryInterval = 1000));
- Auto resubscribe with maximum retries and default 10000ms interval.
const ws = new WS_RPC(config.wsURL);
const provider = new ViteAPI(ws, () => {
console.log("Connected");
}, new RenewSubscription(Number.MAX_VALUE));
- Auto reconnect with unlimited retries and default 10000ms interval.
const ws = new WS_RPC(config.wsURL);
const provider = new ViteAPI(ws, () => {
console.log("Connected");
}, new AlwaysReconnect());
- Use default handler (auto reconnect with 10 retries and 10000ms interval).
const ws = new WS_RPC(config.wsURL);
const provider = new ViteAPI(ws, () => {
console.log("Connected");
});
HTTP
Installation
Starting from 2.3.19, package @vite/vitejs-http is consolidated into @vite/vitejs, so there's no need to install the package separately.
Module Import
HTTP_RPC Class
- Constructor
host:string = 'http://localhost:48132'Connection URLtimeout: number = 60000Timeout in millisecondsoptions: object = { headers: [] }headers: { name: any, value: any }[]Request headers
- Example
IPC
Installation
Starting from 2.3.19, package @vite/vitejs-ipc is consolidated into @vite/vitejs, so there's no need to install the package separately.
Module Import
IPC_RPC Class
- Constructor
path:string = ''File path to gvite.ipctimeout: number = 60000Timeout in millisecondsoptions: objectdelimiter: '\n'The delimiter at the end of each data packet
- Example