From 8b7a6aa9b5d35f2f65b5fced1ea6d4978c2b8b4f Mon Sep 17 00:00:00 2001 From: santiagodevrel Date: Wed, 17 Apr 2024 11:55:51 +0300 Subject: [PATCH 1/6] changed sidebar position --- docs/docs/guides/events_subscriptions/custom_subscriptions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/guides/events_subscriptions/custom_subscriptions.md b/docs/docs/guides/events_subscriptions/custom_subscriptions.md index 6f5826fe902..7dbf1df1990 100644 --- a/docs/docs/guides/events_subscriptions/custom_subscriptions.md +++ b/docs/docs/guides/events_subscriptions/custom_subscriptions.md @@ -1,5 +1,5 @@ --- -sidebar_position: 3 +sidebar_position: 2 sidebar_label: 'Custom Subscriptions' --- From 24deba520168dc5c4797f58188ea72d6f9aa7942 Mon Sep 17 00:00:00 2001 From: santiagodevrel Date: Wed, 17 Apr 2024 11:56:06 +0300 Subject: [PATCH 2/6] removed supported subcription and added it to index.md --- .../supported_subscriptions.md | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 docs/docs/guides/events_subscriptions/supported_subscriptions.md diff --git a/docs/docs/guides/events_subscriptions/supported_subscriptions.md b/docs/docs/guides/events_subscriptions/supported_subscriptions.md deleted file mode 100644 index bb8b704dd62..00000000000 --- a/docs/docs/guides/events_subscriptions/supported_subscriptions.md +++ /dev/null @@ -1,15 +0,0 @@ ---- -sidebar_position: 2 -sidebar_label: 'Supported Subscriptions' ---- - -# Supported Subscriptions - -web3.js supports the standard Ethereum subscriptions out of the box. And they are the ones registered inside [registeredSubscriptions](/api/web3-eth#registeredSubscriptions) object. Here are a list of them: - -- `logs`: implemented in the class [`LogsSubscription`](/api/web3-eth/class/LogsSubscription). -- `newBlockHeaders`: implemented in the class [`NewHeadsSubscription`](/api/web3-eth/class/NewHeadsSubscription). -- `newHeads` same as `newBlockHeaders`. -- `newPendingTransactions`: implemented in the class [`NewPendingTransactionsSubscription`](/api/web3-eth/class/NewPendingTransactionsSubscription). -- `pendingTransactions`: same as `newPendingTransactions`. -- `syncing`: implemented in the class [`SyncingSubscription`](/api/web3-eth/class/SyncingSubscription) From b08389cb931cd96ae0e492197a1e232225090168 Mon Sep 17 00:00:00 2001 From: santiagodevrel Date: Wed, 17 Apr 2024 11:56:21 +0300 Subject: [PATCH 3/6] subcribe to events and node events guide --- .../docs/guides/events_subscriptions/index.md | 146 +++++++++++++++++- 1 file changed, 142 insertions(+), 4 deletions(-) diff --git a/docs/docs/guides/events_subscriptions/index.md b/docs/docs/guides/events_subscriptions/index.md index 7bd2f60b958..00031676aa9 100644 --- a/docs/docs/guides/events_subscriptions/index.md +++ b/docs/docs/guides/events_subscriptions/index.md @@ -1,17 +1,155 @@ --- sidebar_position: 1 -sidebar_label: 'Introduction' +sidebar_label: 'Mastering events subcriptions' --- # Events Subscription +## Subscribing to smart contracts events + +```js +import { Web3 } from "web3"; + +// set a provider - MUST be a WebSocket(WSS) provider +const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); + +async function subscribe() { + // create a new contract object, providing the ABI and address + const contract = new web3.eth.Contract(abi, address); + + // subscribe to the smart contract event + const subscription = contract.events.EventName(); + + // new value every time the event is emitted + subscription.on("data", console.log); +} + +subscribe(); +``` + + +## Subscribing to node events + 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. :::important 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) ::: -## Here are the guides for events subscription -- [Supported Subscriptions Guide](/guides/events_subscriptions/supported_subscriptions) -- [Custom Subscriptions Guide](/guides/events_subscriptions/custom_subscriptions) +- `on("data")` - Fires on each incoming log with the log object as argument. +```js + subcription.on("data", (data) => console.log(data)); +``` + +- `on("changed")` - Fires on each log which was removed from the blockchain. The log will have the additional property "removed: true". +```js + subcription.on("changed", (changed) => console.log(changed)); +``` + +- `on("error")` - Fires when an error in the subscription occurs. +```js + subcription.on("error", (error) => console.log(error)); +``` + +- `on("connected")` - Fires once after the subscription successfully connected. Returns the subscription id. +```js + subcription.on("connected", (connected) => console.log(connected)); +``` +### Logs + +- `logs`: implemented in the class [`LogsSubscription`](/api/web3-eth/class/LogsSubscription) + +```js +import { Web3 } from "web3"; + +const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); + +async function subscribe() { + //create subcription + const subcription = await web3.eth.subscribe("logs"); + + //print logs of the latest mined block + subcription.on("data", (data) => console.log(data)); + + //unsubscribe + //await subcription.unsubscribe(); +} + +subscribe(); +``` + +### Pending Transactions + +- `newPendingTransactions`: implemented in the class [`NewPendingTransactionsSubscription`](/api/web3-eth/class/NewPendingTransactionsSubscription). +- `pendingTransactions`: same as `newPendingTransactions`. + +```js +import { Web3 } from "web3"; + +const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); + +async function subscribe() { + //create subcription + const subcription = await web3.eth.subscribe("pendingTransactions"); //or ("newPendingTransactions") + + //print tx hashs of pending transactions + subcription.on("data", (data) => console.log(data)); + + //unsubscribe + //await subcription.unsubscribe(); +} + +subscribe(); +``` + +### Block headers + +- `newBlockHeaders`: implemented in the class [`NewHeadsSubscription`](/api/web3-eth/class/NewHeadsSubscription). +- `newHeads` same as `newBlockHeaders`. + +```js +import { Web3 } from "web3"; + +const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); + +async function subscribe() { + //create subcription + const subcription = await web3.eth.subscribe("newBlockHeaders"); //or ("newHeads") + + //print block header everytime a block is mined + subcription.on("data", (data) => console.log(data)); + + //unsubscribe + //await subcription.unsubscribe(); +} + +subscribe(); +``` + +### Syncing + +- `syncing`: implemented in the class [`SyncingSubscription`](/api/web3-eth/class/SyncingSubscription) + +```js +import { Web3 } from "web3"; + +const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); + +async function subscribe() { + //create subcription + const subcription = await web3.eth.subscribe("syncing"); + + //this will return `true` when the node is syncing + //when it’s finished syncing will return `false`, for the `changed` event. + subcription.on("data", (data) => console.log(data)); + + //unsubscribe + //await subcription.unsubscribe(); +} + +subscribe(); + +``` + + From c14fcd847371546aaf3868ca6817d8403914d1c5 Mon Sep 17 00:00:00 2001 From: santiagodevrel Date: Wed, 17 Apr 2024 11:59:47 +0300 Subject: [PATCH 4/6] renamed label --- docs/docs/guides/events_subscriptions/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/guides/events_subscriptions/index.md b/docs/docs/guides/events_subscriptions/index.md index 00031676aa9..2af888ad1e3 100644 --- a/docs/docs/guides/events_subscriptions/index.md +++ b/docs/docs/guides/events_subscriptions/index.md @@ -1,6 +1,6 @@ --- sidebar_position: 1 -sidebar_label: 'Mastering events subcriptions' +sidebar_label: 'Mastering Events Subcriptions' --- # Events Subscription From 6e87a0b95b077d7976f086526440f91296738237 Mon Sep 17 00:00:00 2001 From: santiagodevrel Date: Thu, 18 Apr 2024 22:02:36 +0300 Subject: [PATCH 5/6] changed js to ts and added unsubscribe function --- .../docs/guides/events_subscriptions/index.md | 47 ++++++++++++------- 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/docs/docs/guides/events_subscriptions/index.md b/docs/docs/guides/events_subscriptions/index.md index 2af888ad1e3..785ffbd634f 100644 --- a/docs/docs/guides/events_subscriptions/index.md +++ b/docs/docs/guides/events_subscriptions/index.md @@ -7,7 +7,7 @@ sidebar_label: 'Mastering Events Subcriptions' ## Subscribing to smart contracts events -```js +```ts import { Web3 } from "web3"; // set a provider - MUST be a WebSocket(WSS) provider @@ -38,29 +38,29 @@ If you are the developer who provides custom subscriptions to users. We encourag - `on("data")` - Fires on each incoming log with the log object as argument. -```js +```ts subcription.on("data", (data) => console.log(data)); ``` - `on("changed")` - Fires on each log which was removed from the blockchain. The log will have the additional property "removed: true". -```js +```ts subcription.on("changed", (changed) => console.log(changed)); ``` - `on("error")` - Fires when an error in the subscription occurs. -```js +```ts subcription.on("error", (error) => console.log(error)); ``` - `on("connected")` - Fires once after the subscription successfully connected. Returns the subscription id. -```js +```ts subcription.on("connected", (connected) => console.log(connected)); ``` ### Logs - `logs`: implemented in the class [`LogsSubscription`](/api/web3-eth/class/LogsSubscription) -```js +```ts import { Web3 } from "web3"; const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); @@ -71,12 +71,15 @@ async function subscribe() { //print logs of the latest mined block subcription.on("data", (data) => console.log(data)); +} - //unsubscribe - //await subcription.unsubscribe(); +// function to unsubscribe from a subscription +async function unsubscribe(subscription) { + await subscription.unsubscribe(); } subscribe(); +unsuscribe(subscription); ``` ### Pending Transactions @@ -84,7 +87,7 @@ subscribe(); - `newPendingTransactions`: implemented in the class [`NewPendingTransactionsSubscription`](/api/web3-eth/class/NewPendingTransactionsSubscription). - `pendingTransactions`: same as `newPendingTransactions`. -```js +```ts import { Web3 } from "web3"; const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); @@ -95,12 +98,15 @@ async function subscribe() { //print tx hashs of pending transactions subcription.on("data", (data) => console.log(data)); +} - //unsubscribe - //await subcription.unsubscribe(); +// function to unsubscribe from a subscription +async function unsubscribe(subscription) { + await subscription.unsubscribe(); } subscribe(); +unsuscribe(subscription); ``` ### Block headers @@ -108,7 +114,7 @@ subscribe(); - `newBlockHeaders`: implemented in the class [`NewHeadsSubscription`](/api/web3-eth/class/NewHeadsSubscription). - `newHeads` same as `newBlockHeaders`. -```js +```ts import { Web3 } from "web3"; const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); @@ -119,19 +125,22 @@ async function subscribe() { //print block header everytime a block is mined subcription.on("data", (data) => console.log(data)); +} - //unsubscribe - //await subcription.unsubscribe(); +// function to unsubscribe from a subscription +async function unsubscribe(subscription) { + await subscription.unsubscribe(); } subscribe(); +unsuscribe(subscription); ``` ### Syncing - `syncing`: implemented in the class [`SyncingSubscription`](/api/web3-eth/class/SyncingSubscription) -```js +```ts import { Web3 } from "web3"; const web3 = new Web3("wss://ethereum-rpc.publicnode.com"); @@ -143,13 +152,15 @@ async function subscribe() { //this will return `true` when the node is syncing //when it’s finished syncing will return `false`, for the `changed` event. subcription.on("data", (data) => console.log(data)); +} - //unsubscribe - //await subcription.unsubscribe(); +// function to unsubscribe from a subscription +async function unsubscribe(subscription) { + await subscription.unsubscribe(); } subscribe(); - +unsuscribe(subscription); ``` From a9e42902efff47580f9c7b063472154bb89615ae Mon Sep 17 00:00:00 2001 From: santiagodevrel Date: Thu, 18 Apr 2024 22:11:23 +0300 Subject: [PATCH 6/6] typos --- docs/docs/guides/events_subscriptions/index.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/docs/guides/events_subscriptions/index.md b/docs/docs/guides/events_subscriptions/index.md index 785ffbd634f..445e7edc568 100644 --- a/docs/docs/guides/events_subscriptions/index.md +++ b/docs/docs/guides/events_subscriptions/index.md @@ -24,7 +24,13 @@ async function subscribe() { subscription.on("data", console.log); } +// function to unsubscribe from a subscription +async function unsubscribe(subscription) { + await subscription.unsubscribe(); +} + subscribe(); +unsubscribe(subscription); ``` @@ -79,7 +85,7 @@ async function unsubscribe(subscription) { } subscribe(); -unsuscribe(subscription); +unsubscribe(subscription); ``` ### Pending Transactions @@ -106,7 +112,7 @@ async function unsubscribe(subscription) { } subscribe(); -unsuscribe(subscription); +unsubscribe(subscription); ``` ### Block headers @@ -133,7 +139,7 @@ async function unsubscribe(subscription) { } subscribe(); -unsuscribe(subscription); +unsubscribe(subscription); ``` ### Syncing @@ -160,7 +166,7 @@ async function unsubscribe(subscription) { } subscribe(); -unsuscribe(subscription); +unsubscribe(subscription); ```