Web3.js: web3.eth.subscribe not working

Created on 6 Oct 2017  路  14Comments  路  Source: ChainSafe/web3.js

Running geth with websocket:

geth --fast --cache=512 --ws --wsorigins="*" --wsapi "db,eth,net,ssh,miner,web3,personal,admin"

Then connecting with web3 in a Node.js app:

const Web3 = require('web3');

var web3 = new Web3(new Web3.providers.WebsocketProvider("ws://localhost:8546"));

console.log(web3.version);

web3.eth.subscribe('pendingTransactions', function(err, res) {
    console.log('Here')
    console.log(err)
    console.log(res)
}).on('data', function(transaction) {
    console.log('Here 2')
    console.log(transaction)
});

"Here", "Here 2" do not print to the console. The subscription does not seem to activate, nothing is printed after the version prints to the console. web3.version and other web3 commands work fine.


Geth version: 1.7.1-stable

Web3 version: 1.0.0-beta.22

bug

Most helpful comment

Please make sure you are synced up. While geth is doing its fast sync, the chain is incomplete, so there aren't any events being fired, since there's no existing events yet. Only when the chain sync fully will events start appearing.

All 14 comments

I have the same issue. I don't get any messages for either pendingTransactions or newBlockHeaders subscriptions.

Geth version: 1.7.2-stable
Web3 version: 1.0.0-beta.23 ( and also 1.0.0-beta.24 )

I don't receive any callbacks on any subscriptions either.
Web3 version: 1.0.0-beta.24
TestRPC 4.1.3

@Lytigas @johnnyboo can you please try something for me? Connect using wscat to the socket and try these commands. Are you getting any notifications back? If not it might be an issue with geth and not web3.

I have seen the same thing with [email protected] and [email protected].

Please make sure you are synced up. While geth is doing its fast sync, the chain is incomplete, so there aren't any events being fired, since there's no existing events yet. Only when the chain sync fully will events start appearing.

I have the same issue - it dosn't seem to work on geth 1.7.2 (fast sync) - has anybody been able to resolve this yet ?

same issue here -
geth version 1.7.2 (without fast sync)
web3 version 1.0.0-beta.24

wscat -c ws://some.host.com:8546
returns:

error: Error: unexpected server response (403)

Have you ensured that you are passing --wsorigins, with the host you wish to use? As that 403 may be a failed handshake.

@MrTibbles this is the command I'm running geth:
geth --rpc --ws --wsaddr 0.0.0.0 --wsorigins="*" --testnet --rpcaddr 0.0.0.0

ok, so I've solved the issue running:

geth --rpc --rpccorsdomain="*" --ws --wsorigins="*" --wsaddr 0.0.0.0 --testnet --rpcaddr 0.0.0.0

Note: need to set BOTH rpccorsdomain & wsorigins

Sorry, completely missed your initial response. I was close though, that being handshake failed :)

@MrTibbles thanks for the help 馃憤

Not working for me:

  const Web3 = require('web3');
  const web3 = new Web3(new Web3.providers.WebsocketProvider(process.env.GETH_WS_URL));
  console.log(process.env.GETH_WS_URL);
  const syncSubscription = web3.eth.subscribe('syncing')
    .on('data', (tx) => {
      console.log('tx', tx);
    });

  console.log(syncSubscription);

I see here and the syncSubscription object, but nothing in the on('data

I'm launching with geth --rpc --syncmode fast --rpcaddr 0.0.0.0 --rpcapi web3,eth --ws --wsaddr 0.0.0.0 --wsapi web3,eth --wsorigins '*'

This should be fixed with the PR #2000

Was this page helpful?
0 / 5 - 0 ratings