Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 7008e5c

Browse files
authored
update docs (#7318)
1 parent bbde6ea commit 7008e5c

File tree

1 file changed

+58
-21
lines changed

1 file changed

+58
-21
lines changed

docs/docs/guides/15_web3_upgrade_guide/providers_migration_guide.md

Lines changed: 58 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ sidebar_label: web3.providers
66

77
For full description about the providers, their priorities and their types, you can check [web3.js Providers Guide](/guides/web3_providers_guide/).
88

9-
### Provider Options Changes
9+
## Provider Options Changes
1010

11-
There are differences in the objects that could be passed in the Provider constructors between version 1.x and 4.x. Below, you will find the difference for every Provider object type.
11+
There are differences in the objects that could be passed in the Provider constructors between version 1.x and v4. Below, you will find the difference for every Provider object type.
1212

13-
#### HttpProvider
13+
### HttpProvider
14+
15+
#### HttpProviderOptions
1416

1517
In 1.x, options passed in the constructor should be of type [`HttpProviderOptions`](https://github.com/web3/web3.js/blob/1.x/packages/web3-core-helpers/types/index.d.ts#L173). The `HttpProviderOptions` interface consists of:
1618

@@ -35,7 +37,7 @@ interface HttpHeader {
3537
}
3638
```
3739

38-
In 4.x, the options is of type `HttpProviderOptions`, which is an object with a `providerOptions` key and value a `RequestInit` object.
40+
In v4, the options is of type `HttpProviderOptions`, which is an object with a `providerOptions` key and value a `RequestInit` object.
3941
Regarding `RequestInit` see [microsoft's github](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules_typedoc_node_modules_typescript_lib_lib_dom_d_.requestinit.html).
4042

4143
For example:
@@ -58,7 +60,7 @@ let httpOptions = {
5860
}
5961
};
6062

61-
// in 4.x
63+
// in v4
6264
let httpOptions = {
6365
providerOptions: {
6466
body: undefined,
@@ -80,7 +82,9 @@ let httpOptions = {
8082
};
8183
```
8284

83-
#### WebSocketProvider
85+
### WebSocketProvider
86+
87+
#### WebsocketProviderOptions
8488

8589
In 1.x, options passed in the constructor should be of type [`WebsocketProviderOptions`](https://github.com/web3/web3.js/blob/1.x/packages/web3-core-helpers/types/index.d.ts#L192). The `WebsocketProviderOptions` interface consists of:
8690

@@ -105,9 +109,9 @@ interface ReconnectOptions {
105109
}
106110
```
107111

108-
In 4.x, the `socketOptions` parameter is of type `ClientRequestArgs` or of `ClientOptions`. See [here](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules__types_node_http_d_._http_.clientrequestargs.html) for `ClientRequestArgs` and [here](https://github.com/websockets/ws) for `ClientOptions`.
112+
In v4, the `socketOptions` parameter is of type `ClientRequestArgs` or of `ClientOptions`. See [here](https://microsoft.github.io/PowerBI-JavaScript/interfaces/_node_modules__types_node_http_d_._http_.clientrequestargs.html) for `ClientRequestArgs` and [here](https://github.com/websockets/ws) for `ClientOptions`.
109113

110-
In 4.x the `reconnectOptions` parameter can be given to control: auto-reconnecting, delay and max tries attempts. And here is its type:
114+
In v4 the `reconnectOptions` parameter can be given to control: auto-reconnecting, delay and max tries attempts. And here is its type:
111115

112116
```ts
113117
// this is the same options interface used for both WebSocketProvider and IpcProvider
@@ -118,7 +122,36 @@ type ReconnectOptions = {
118122
};
119123
```
120124

121-
##### Options examples
125+
#### Program Behavior with WebSocket Provider
126+
127+
In 1.x, a program connected to a WebSocket provider would automatically terminate after executing all code, even if the WebSocket connection was still active:
128+
129+
```ts
130+
const web3 = new Web3(wsUrl);
131+
// The program will terminate after connecting since there are no more lines of code to run.
132+
```
133+
134+
In version 4.x, this behavior has changed. When connected to a WebSocket provider, the program will not automatically terminate after the code finishes running. This is to ensure the WebSocket connection remains open, allowing the program to continue listening for events.
135+
136+
The program will remain active until you explicitly disconnect from the WebSocket provider:
137+
138+
```ts
139+
const web3 = new Web3(wsUrl);
140+
// The program will keep running to listen for events.
141+
```
142+
143+
#### Terminating the Program
144+
145+
When you're ready to stop the program, you can manually disconnect from the WebSocket provider by calling the disconnect method. This will properly close the connection and terminate the program:
146+
147+
```ts
148+
const web3 = new Web3(wsUrl);
149+
// When you are ready to terminate your program
150+
web3.currentProvider?.disconnect();
151+
// The program will now terminate
152+
```
153+
154+
### Options examples
122155

123156
Below is an example for the passed options for each version:
124157

@@ -150,7 +183,7 @@ var options = {
150183
onTimeout: false,
151184
},
152185
};
153-
// in 4.x
186+
// in v4
154187
let clientOptions: ClientOptions = {
155188
// Useful for credentialed urls, e.g: ws://username:password@localhost:8546
156189
headers: {
@@ -199,7 +232,9 @@ const provider = new WebSocketProvider(
199232
);
200233
```
201234

202-
#### Legacy Event `close` has been deprecated
235+
### Legacy Event `close` has been deprecated
236+
237+
#### Disconnect and close event
203238

204239
Following EIP-1193, the `close` event has been deprecated and is superceded by `disconnect`.
205240
In 1.x, we listen for a `close` event:
@@ -215,7 +250,7 @@ provider.disconnect(1012);
215250
// would eventually log closed
216251
```
217252

218-
In 4.x, we listen for a `disconnect` event:
253+
In v4, we listen for a `disconnect` event:
219254

220255
```ts
221256
const provider = new WebSocketProvider(host + port);
@@ -228,7 +263,9 @@ provider.disconnect(1012);
228263
// would eventually log 'closed'
229264
```
230265

231-
#### IpcProvider
266+
### IpcProvider
267+
268+
#### Using IpcProvider
232269

233270
The IPC provider is used in node.js dapps when running a local node. And it provide the most secure connection.
234271

@@ -240,7 +277,7 @@ import * as net from 'net';
240277
const ipcProvider = new IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', new net.Server());
241278
```
242279

243-
In 4.x, it's no longer installed by default as its nodejs modules are impacting web3.js browser usage.
280+
In v4, it's no longer installed by default as its nodejs modules are impacting web3.js browser usage.
244281
You can use it by installing `web3-providers-ipc` and creating a new instance. Since it's compatible with Eip1193Provider,
245282
you can pass it on to the Web3 instance.
246283

@@ -262,7 +299,7 @@ interface SocketConstructorOpts {
262299
}
263300
```
264301

265-
In 4.x the third parameter is called `reconnectOptions` that is of the type `ReconnectOptions`. It can be given to control: auto-reconnecting, delay and max tries attempts. And here its type:
302+
In v4 the third parameter is called `reconnectOptions` that is of the type `ReconnectOptions`. It can be given to control: auto-reconnecting, delay and max tries attempts. And here its type:
266303

267304
```ts
268305
// this is the same options interface used for both WebSocketProvider and IpcProvider
@@ -285,7 +322,7 @@ new Web3.providers.IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc', net);
285322
// on windows the path is: "\\\\.\\pipe\\geth.ipc"
286323
// on linux the path is: "/users/myuser/.ethereum/geth.ipc"
287324

288-
// in 4.x
325+
// in v4
289326
let clientOptions: SocketConstructorOpts = {
290327
allowHalfOpen: false;
291328
readable: true;
@@ -333,7 +370,7 @@ const provider = new IpcProvider(
333370
);
334371
```
335372

336-
#### Error message for reconnect attempts
373+
### Error message for reconnect attempts
337374

338375
:::note
339376
This section applies for both `IpcProvider` and `WebSocketProvider`.
@@ -342,11 +379,11 @@ This section applies for both `IpcProvider` and `WebSocketProvider`.
342379
The error in, version 1.x, was an Error object that contains the message:
343380
`'Maximum number of reconnect attempts reached!'`
344381

345-
And, the error in version 4.x, is the same, but will also contain the value of the variable `maxAttempts` as follows:
382+
And, the error in version v4, is the same, but will also contain the value of the variable `maxAttempts` as follows:
346383

347384
`` `Maximum number of reconnect attempts reached! (${maxAttempts})` ``
348385

349-
And here is how to catch the error, in version 4.x, if max attempts reached when there is auto reconnecting:
386+
And here is how to catch the error, in version v4, if max attempts reached when there is auto reconnecting:
350387

351388
```ts
352389
provider.on('error', error => {
@@ -357,7 +394,7 @@ provider.on('error', error => {
357394
});
358395
```
359396

360-
#### Legacy Event `close` has been deprecated
397+
### Legacy Event `close` has been deprecated
361398

362399
Following EIP-1193, the `close` event has been deprecated and is superseded by `disconnect`.
363400
In 1.x, we listen for a `close` event:
@@ -373,7 +410,7 @@ provider.disconnect(1012);
373410
// would eventually log closed
374411
```
375412

376-
In 4.x, we listen for a `disconnect` event:
413+
In v4, we listen for a `disconnect` event:
377414

378415
```ts
379416
const provider = new IpcProvider(host + port);

0 commit comments

Comments
 (0)