-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WebSocketProvider subscribing to events does not work #6094
Comments
Same problem, except mine does not work with type "logs" |
#4618 ? |
Sorry, there is no issue with the provider. Actually, the way how subscribing to data had changed in version 4.x. Here is how it works with version 4.x: const { Web3 } = require('web3');
// Connect to the Ethereum network using WebSocket provider
const ganacheUrl = 'ws://localhost:8545';
const wsProvider = new Web3.providers.WebsocketProvider(ganacheUrl);
const web3 = new Web3(wsProvider);
async function main() {
try {
console.log(
'Does the provider support subscriptions?:',
wsProvider.supportsSubscriptions(),
);
// Subscribe to new block headers
const subscription = await web3.eth.subscribe('newBlockHeaders');
subscription.on("data", async (blockhead) => {
console.log('New block header: ', blockhead);
// You do not need the next line if you like to keep notified for every new block
await subscription.unsubscribe();
console.log('Unsubscribed from new block headers.');
});
subscription.on("error", (error) =>
console.log('Error when subscribing to New block header: ', error)
);
// Get the list of accounts in the connected node which is in this case: Ganache.
const accounts = await web3.eth.getAccounts();
// Send a transaction to the network
const transactionReceipt = await web3.eth.sendTransaction({
from: accounts[0],
to: accounts[1],
value: web3.utils.toWei('0.001', 'ether')
});
console.log('Transaction Receipt:', transactionReceipt);
} catch (error) {
console.error(error);
}
}
main(); However, since we missed this point. It seems we need to emphasize it in the documentation. Additionally, I spotted one or two places in the documentation where the old style is used. So, I will open an MR to update the documentation. |
Expected behavior
To be able to subscribe to events.
Actual behavior
The WebSocketProvider does not notify for new events. Actually, the same behavior happened with all events. However, the example below is with
newBlockHeaders
.(Note; I am not sure if IpcProvider has the same problem. It should be checked).
Steps to reproduce the behavior
The following works with version 1.10:
Logs
In version 1.10, the following is printed and the program ends with no issues:
However, for the same piece of code, in version
4.0.1-rc.1
, the output is as follows and the execution hangs without finishing:Environment
node.js
The text was updated successfully, but these errors were encountered: