1

Using web3.js version 1(beta) and using the WebsocketProvider to make a connection via

const Web3 = require('web3')

const web3 = new Web3('ws://localhost:8546')
const provider = web3.providers.WebsocketProvider

const { connection } = provider

==> connection is undefined

looking at the source for the WebsocketProvider I can see it's got a connection object

function WebsocketProvider(url)  {
  var _this = this;
  this.responseCallbacks = {};
  this.notificationCallbacks = [];
  this.connection = new Ws(url);

  this.addDefaultEvents();

  // LISTEN FOR CONNECTION RESPONSES
  this.connection.onmessage = function(e) {
    /*jshint maxcomplexity: 6 */
    var data = (typeof e.data === 'string') ? e.data : '';

    _this._parseResponse(data).forEach(function(result){

      var id = null;

      // get the id which matches the returned id
      if(_.isArray(result)) {
          result.forEach(function(load){
              if(_this.responseCallbacks[load.id])
                  id = load.id;
          });
      } else {
          id = result.id;
      }

      // notification
      if(!id && result.method.indexOf('_subscription') !== -1) {
          _this.notificationCallbacks.forEach(function(callback){
              if(_.isFunction(callback))
                  callback(null, result);
          });

          // fire the callback
      } else if(_this.responseCallbacks[id]) {
          _this.responseCallbacks[id](null, result);
          delete _this.responseCallbacks[id];
      }
    });
  };
}

Curiously I can't actually find the source code for this in GitHub, the above comes by logging web3.providers.WebsocketProvider

Questions

  1. At what point is the connection actually made,
  2. how can I expose it such that I can attach error and other handlers to make a more reliable connection, and
  3. Where is the source code for this, given it's nowhere to be found in github.com/ethereum/web3.js

2 Answers 2

2

Let me answer each of your questions:

1. At what point is the connection actually made

The connection is actually made after creating a WebsocketProvider object. Taking a snippet from your example:

const web3 = new Web3('ws://localhost:8546')
const provider = web3.providers.WebsocketProvider

const { connection } = provider

At this point, the connection object is yet to be initialized. Here is what you should have done instead:

const provider = new web3.providers.WebsocketProvider('ws://localhost:8546')
const { connection } = provider

// then you can initialize your web3 with the provider like so
const web3 = new Web3(provider)

2. how can I expose it such that I can attach error and other handlers to make a more reliable connection, and

Now you have access to the connection object, you can attach your own listener to the onmessage, onerror or onclose. BUT, I'll advise you don't do that. I'm not sure what your use case is for this, but the WebsocketProvider class handles subscription to this event handlers pretty well. If you are having a reliability issue with the websocket provider you are connected to, you might want to change the connection to another one.

3. Where is the source code for this, given it's nowhere to be found in github.com/ethereum/web3.js

The code is actually in the repository you pointed to. You might want to switch to the 1.0 branch to see the correct code for [email protected].

1

Where is the source code for this, given it's nowhere to be found in github.com/ethereum/web3.js

https://github.com/HaoLLL/web3-eth-contract/blob/master/src/index.js

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.