1

I have a little script just with the following function:

const Web3 = require('web3');
var web3Provider = new Web3(new Web3.providers.WebsocketProvider('ws://127.0.0.1:3001'));
const web3 = new Web3(web3Provider);

web3.eth.getAccounts().then(e => console.log(e));

But when I receive the callback, the script is never closed, it continues running (doing nothing). How can I stoped it? why it happens?

2

Unlike an HTTP connection, a WebSocket needs to be closed explicitly.

You can do it with:

web3.currentProvider.connection.close();

Or more generally, with:

if (web3.currentProvider.constructor.name == "WebsocketProvider")
    web3.currentProvider.connection.close();

The latter will allow you to support both types of connection in your script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.