3

I receive this error when I'm trying to connect to the websocket

WebSocket connection to 'wss://ropsten.infura.io/ws' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

Code snippet

import web3 from 'web3';
var web3lib = new web3(
  new web3.providers.WebsocketProvider('wss://ropsten.infura.io/ws'),
);  
console.log(
  'provider in web3 eth: ' + JSON.stringify(web3lib.currentProvider),
);  
console.log('New block in web3 eth: ' + web3lib.blockNumber);
var subscription = web3lib.eth.subscribe('newBlockHeaders', function(
  error,
  result,
) { 
   if (!error)
       console.log("output of web3 stream"+JSON.stringify(result));
       else
  console.log('Error of web3 stream ' + JSON.stringify(result));
}); 

What am I doing wrong here?

4 Answers 4

5

was fixed on [email protected] https://github.com/ethereum/web3.js/issues/1559#issuecomment-412348642

4

This is working

#!/usr/bin/env node
const Web3 = require('web3');

const web3 = new Web3(new Web3.providers.WebsocketProvider('wss://mainnet.infura.io/ws'));

const subscription = web3.eth.subscribe('newBlockHeaders', (error, blockHeader) => {
if (error) return console.error(error);

console.log('Successfully subscribed!', blockHeader);
}).on('data', (blockHeader) => {
console.log('data: ', blockHeader);
});

// unsubscribes the subscription
subscription.unsubscribe((error, success) => {
if (error) return console.error(error);

console.log('Successfully unsubscribed!');
});

Took this example provided by https://github.com/egalano

https://github.com/INFURA/infura/issues/29#issuecomment-365128914

6
  • I am again getting the same error for this piece of code also. I am using web3.js 1.0.0-beta.34. Would that be a problem or else what to check?
    – Sundar
    Commented Jul 20, 2018 at 13:58
  • Did you try with any other version? Commented Jul 22, 2018 at 13:08
  • 0.20.6 version worked on HTTP and it doesn't have Websocket. Which version are you using?
    – Sundar
    Commented Jul 23, 2018 at 10:32
  • [email protected] Commented Jul 23, 2018 at 10:42
  • I updated to [email protected] and started getting the same error. Wouldn't happen with beta-34 or beta-33
    – Gus
    Commented Aug 1, 2018 at 18:55
3

It seems a value of WebSocket request header is invalid.

More specifically say, a value of Sec-WebSocket-Protocol is undefined.

According to the github issue below, the issue was introduced from [email protected]

https://github.com/ethereum/web3.js/issues/1559

I confirmed [email protected] has not fixed the issue yet in my environment. So downgrading to [email protected] is needed if you use WebSocket at this moment.

2
  • 1
    Yeah downgrading worked for me. Thanks for the find.
    – Sundar
    Commented Aug 9, 2018 at 12:42
  • Still now also?
    – mesqueeb
    Commented Sep 6, 2018 at 8:06
0

using your infrua ID to replace MYID :

client, err := ethclient.Dial("wss://mainnet.infura.io/ws/v3/MYID") 

if err != nil {
    log.Fatal(err)
}

headers := make(chan *types.Header)
sub, err := client.SubscribeNewHead(context.Background(), headers)
if err != nil {
    log.Fatal(err)
}

for {
    select {
    case err := <-sub.Err():
        log.Fatal(err)
    case header := <-headers:
        fmt.Println(header.Hash().Hex()) // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f

        block, err := client.BlockByHash(context.Background(), header.Hash())
        if err != nil {
            log.Fatal(err)
        }

        log.Printf("TX:%s", block.Hash().Hex())          // 0xbc10defa8dda384c96a17640d84de5578804945d347072e091b4e5f390ddea7f
        log.Printf("Height:%d", block.Number().Uint64()) // 3477413
        log.Printf("Time:%d", block.Time())              // 1529525947
        log.Println(block.Nonce())                       // 130524141876765836
        log.Printf("#:%d", len(block.Transactions()))    // 7
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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