I have a little script just with the following function:

const Web3 = require('web3');
var web3Provider = new Web3(new Web3.providers.WebsocketProvider('ws://'));
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?


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

You can do it with:


Or more generally, with:

if (web3.currentProvider.constructor.name == "WebsocketProvider")

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

| improve this answer | |

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.