Already referred web3 documentations and other Q-A for web socket connection. Currently using infura as provider.

  • Please mark the question as solved if I helped you – Niklas Feurstein Aug 30 '19 at 14:50
  • Do you need further guidance or is this question completed? – Niklas Feurstein Sep 2 '19 at 15:12
  • @NiklasFeurstein, further discussion available at Discussion Room. – Rudrika Sep 3 '19 at 5:42

I think this is what you are looking for:

const Web3 = require('web3');
const web3 = new Web3('wss://ropsten.infura.io/ws/v3/<yourID>');

const abi = "placeYourABI";

const address = 'yourContractAddress';

const contract = new web3.eth.Contract(abi, address);

contract.getPastEvents('allEvents', {
    fromBlock: 0,
    toBlock: 'latest'

Replace the variables. Take note of the URL. Websocket URL is not only different by wss at the front.

This code is tested and surely works.

EDIT: Please write why you downvote this answer! It is completely correct.

  • How can I check that this connection is open or not? – Rudrika Aug 30 '19 at 9:58
  • Every time it returns, connection not open on send() Error: connection not open – Rudrika Aug 30 '19 at 10:03
  • Check if your node or ganache-cli process listening at the endpoint provided is up – Sanjay S B Aug 30 '19 at 11:21
  • @Rudrika I updated my answer – Niklas Feurstein Aug 30 '19 at 11:34
  • @Pave, it gives, "connection not open on send()" – Rudrika Aug 30 '19 at 11:47

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.