Thanks to visit codestin.com
Credit goes to github.com

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

Commit dd172c7

Browse files
Santiago Trujillo ZuluagaAlex
andauthored
Events guide 6741 (#6983)
* changed sidebar position * removed supported subcription and added it to index.md * subcribe to events and node events guide * renamed label * changed js to ts and added unsubscribe function * typos --------- Co-authored-by: Alex <[email protected]>
1 parent 93296c2 commit dd172c7

File tree

3 files changed

+160
-20
lines changed

3 files changed

+160
-20
lines changed

docs/docs/guides/events_subscriptions/custom_subscriptions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 3
2+
sidebar_position: 2
33
sidebar_label: 'Custom Subscriptions'
44
---
55

Lines changed: 159 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,172 @@
11
---
22
sidebar_position: 1
3-
sidebar_label: 'Introduction'
3+
sidebar_label: 'Mastering Events Subcriptions'
44
---
55

66
# Events Subscription
77

8+
## Subscribing to smart contracts events
9+
10+
```ts
11+
import { Web3 } from "web3";
12+
13+
// set a provider - MUST be a WebSocket(WSS) provider
14+
const web3 = new Web3("wss://ethereum-rpc.publicnode.com");
15+
16+
async function subscribe() {
17+
// create a new contract object, providing the ABI and address
18+
const contract = new web3.eth.Contract(abi, address);
19+
20+
// subscribe to the smart contract event
21+
const subscription = contract.events.EventName();
22+
23+
// new value every time the event is emitted
24+
subscription.on("data", console.log);
25+
}
26+
27+
// function to unsubscribe from a subscription
28+
async function unsubscribe(subscription) {
29+
await subscription.unsubscribe();
30+
}
31+
32+
subscribe();
33+
unsubscribe(subscription);
34+
```
35+
36+
37+
## Subscribing to node events
38+
839
A standard Ethereum node like [Geth supports subscribing to specific events](https://geth.ethereum.org/docs/interacting-with-geth/rpc/pubsub#supported-subscriptions). Additionally, there are some Ethereum nodes that provide additional custom subscriptions. As you can find in [Supported Subscriptions](/guides/events_subscriptions/supported_subscriptions) guide, web3.js enables you to subscribe to the standard events out of the box. And it also provides you with the capability to subscribe to custom subscriptions as you can find in the [Custom Subscriptions](/guides/events_subscriptions/custom_subscriptions) guide.
940

1041
:::important
1142
If you are the developer who provides custom subscriptions to users. We encourage you to develop a web3.js Plugin after you go through the [Custom Subscription](#custom-subscription) section below. You can find how to develop a plugin at [web3.js Plugin Developer Guide](/guides/web3_plugin_guide/plugin_authors)
1243
:::
1344

14-
## Here are the guides for events subscription
1545

16-
- [Supported Subscriptions Guide](/guides/events_subscriptions/supported_subscriptions)
17-
- [Custom Subscriptions Guide](/guides/events_subscriptions/custom_subscriptions)
46+
- `on("data")` - Fires on each incoming log with the log object as argument.
47+
```ts
48+
subcription.on("data", (data) => console.log(data));
49+
```
50+
51+
- `on("changed")` - Fires on each log which was removed from the blockchain. The log will have the additional property "removed: true".
52+
```ts
53+
subcription.on("changed", (changed) => console.log(changed));
54+
```
55+
56+
- `on("error")` - Fires when an error in the subscription occurs.
57+
```ts
58+
subcription.on("error", (error) => console.log(error));
59+
```
60+
61+
- `on("connected")` - Fires once after the subscription successfully connected. Returns the subscription id.
62+
```ts
63+
subcription.on("connected", (connected) => console.log(connected));
64+
```
65+
### Logs
66+
67+
- `logs`: implemented in the class [`LogsSubscription`](/api/web3-eth/class/LogsSubscription)
68+
69+
```ts
70+
import { Web3 } from "web3";
71+
72+
const web3 = new Web3("wss://ethereum-rpc.publicnode.com");
73+
74+
async function subscribe() {
75+
//create subcription
76+
const subcription = await web3.eth.subscribe("logs");
77+
78+
//print logs of the latest mined block
79+
subcription.on("data", (data) => console.log(data));
80+
}
81+
82+
// function to unsubscribe from a subscription
83+
async function unsubscribe(subscription) {
84+
await subscription.unsubscribe();
85+
}
86+
87+
subscribe();
88+
unsubscribe(subscription);
89+
```
90+
91+
### Pending Transactions
92+
93+
- `newPendingTransactions`: implemented in the class [`NewPendingTransactionsSubscription`](/api/web3-eth/class/NewPendingTransactionsSubscription).
94+
- `pendingTransactions`: same as `newPendingTransactions`.
95+
96+
```ts
97+
import { Web3 } from "web3";
98+
99+
const web3 = new Web3("wss://ethereum-rpc.publicnode.com");
100+
101+
async function subscribe() {
102+
//create subcription
103+
const subcription = await web3.eth.subscribe("pendingTransactions"); //or ("newPendingTransactions")
104+
105+
//print tx hashs of pending transactions
106+
subcription.on("data", (data) => console.log(data));
107+
}
108+
109+
// function to unsubscribe from a subscription
110+
async function unsubscribe(subscription) {
111+
await subscription.unsubscribe();
112+
}
113+
114+
subscribe();
115+
unsubscribe(subscription);
116+
```
117+
118+
### Block headers
119+
120+
- `newBlockHeaders`: implemented in the class [`NewHeadsSubscription`](/api/web3-eth/class/NewHeadsSubscription).
121+
- `newHeads` same as `newBlockHeaders`.
122+
123+
```ts
124+
import { Web3 } from "web3";
125+
126+
const web3 = new Web3("wss://ethereum-rpc.publicnode.com");
127+
128+
async function subscribe() {
129+
//create subcription
130+
const subcription = await web3.eth.subscribe("newBlockHeaders"); //or ("newHeads")
131+
132+
//print block header everytime a block is mined
133+
subcription.on("data", (data) => console.log(data));
134+
}
135+
136+
// function to unsubscribe from a subscription
137+
async function unsubscribe(subscription) {
138+
await subscription.unsubscribe();
139+
}
140+
141+
subscribe();
142+
unsubscribe(subscription);
143+
```
144+
145+
### Syncing
146+
147+
- `syncing`: implemented in the class [`SyncingSubscription`](/api/web3-eth/class/SyncingSubscription)
148+
149+
```ts
150+
import { Web3 } from "web3";
151+
152+
const web3 = new Web3("wss://ethereum-rpc.publicnode.com");
153+
154+
async function subscribe() {
155+
//create subcription
156+
const subcription = await web3.eth.subscribe("syncing");
157+
158+
//this will return `true` when the node is syncing
159+
//when it’s finished syncing will return `false`, for the `changed` event.
160+
subcription.on("data", (data) => console.log(data));
161+
}
162+
163+
// function to unsubscribe from a subscription
164+
async function unsubscribe(subscription) {
165+
await subscription.unsubscribe();
166+
}
167+
168+
subscribe();
169+
unsubscribe(subscription);
170+
```
171+
172+

docs/docs/guides/events_subscriptions/supported_subscriptions.md

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)