How to use web3-providers-ws - 6 common examples

To help you get started, we’ve selected a few web3-providers-ws examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github ostdotcom / base / lib / ost_web3 / ost-web3-providers-ws.js View on Github external
*/
OstWSProvider.ManagedEventsMap = {
  connect: { managedEvent: 'connect', callback: 'onopen', ostCallback: 'onConnectionOpen' },
  end: { managedEvent: 'end', callback: 'onclose', ostCallback: 'onConnectionClose' },
  error: { managedEvent: 'error', callback: 'onerror', ostCallback: 'onConnectionError' },
  dead: { managedEvent: 'dead', callback: null, ostCallback: null }
};

//An array of Managed Event Names. (Useful to automate the code).
OstWSProvider.ManagedEvents = Object.keys(OstWSProvider.ManagedEventsMap);

// END: Static Stuff

// BEGIN : Prototype Stuff Begins
// Derive the prototype.
OstWSProvider.prototype = Object.create(WebsocketProvider.prototype);

// BEGIN : Declare new props if needed.
// These properties will be initialised by constructor.
OstWSProvider.prototype.endPointUrl = null;
OstWSProvider.prototype.eventEmitter = null;
OstWSProvider.prototype.options = null;
// END: New Props.

// BEGIN : Declare New Methods if needed.
// Helper Method to emit event.
OstWSProvider.prototype.emitEvent = function(eventName, args) {
  const oThis = this;

  // Check if we have listeners. This check is important for 'error' event.
  const eventNames = oThis.eventEmitter.eventNames();
  if (eventNames.indexOf(eventName) < 0) {
github ostdotcom / base / lib / ost_web3 / ost-web3-providers-ws.js View on Github external
logger.error('Failed to reconnect WebSocket with endpoint ', oThis.endPointUrl);

  //Emit Dead Event
  oThis.emitEvent('dead', [oThis]);

  if (oThis.options.killOnReconnectFailure) {
    logger.warn('Shutting down proccess');
    process.kill(process.pid, 'SIGTERM');
  }
};

// END: New Methods.

// BEGIN : Override methods as needed.
//addDefaultEvents
const _base_addDefaultEvents = WebsocketProvider.prototype.addDefaultEvents;
OstWSProvider.prototype.addDefaultEvents = function() {
  const oThis = this;

  //Call the super method.
  _base_addDefaultEvents.apply(oThis, arguments);

  const connection = oThis.connection;

  //Meta Info of connection callback to manage.
  var eventMeta,
    //Name of event to emit. E.g. "end" when connection ends. (Note: web3js decides this).
    eventName,
    // Same as eventName, but, is read from managedEvent.
    // This is useful to change the emmited event name. Can also be set to null if you wish to prevent emiting event.
    managedEventName,
    //Function Name of callback. E.g: "onclose" for connection.onclose
github MyEtherWallet / MyEtherWallet / src / wallets / web3-provider / providers / ws-provider.js View on Github external
constructor(host, options, store, eventHub) {
    this.wsProvider = new Web3WSProvider(host, options);
    this.oWSProvider = new Web3WSProvider(host, options);
    this.lastMessage = new Date().getTime();
    const keepAlive = () => {
      if (
        this.oWSProvider.connection.readyState ===
        this.oWSProvider.connection.OPEN
      )
        this.wsProvider.connection.send(
          '{"jsonrpc":"2.0","method":"net_version","params":[],"id":0}'
        );
      if (
        this.wsProvider.connection.readyState ===
        this.wsProvider.connection.OPEN
      )
        this.oWSProvider.connection.send(
          '{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}'
        );
github MyEtherWallet / MyEtherWallet / src / wallets / web3-provider / providers / ws-provider.js View on Github external
constructor(host, options, store, eventHub) {
    this.wsProvider = new Web3WSProvider(host, options);
    this.oWSProvider = new Web3WSProvider(host, options);
    this.lastMessage = new Date().getTime();
    const keepAlive = () => {
      if (
        this.oWSProvider.connection.readyState ===
        this.oWSProvider.connection.OPEN
      )
        this.wsProvider.connection.send(
          '{"jsonrpc":"2.0","method":"net_version","params":[],"id":0}'
        );
      if (
        this.wsProvider.connection.readyState ===
        this.wsProvider.connection.OPEN
      )
        this.oWSProvider.connection.send(
          '{"jsonrpc":"2.0","method":"net_version","params":[],"id":1}'
github ostdotcom / base / lib / ost_web3 / ost-web3-providers-ws.js View on Github external
const OstWSProvider = (module.exports = function(url, i_d_k, options) {
  const oThis = this;

  //Note Down url
  oThis.endPointUrl = url;

  //Take Care of Options
  oThis.options = Object.assign({}, OstWSProvider.DefaultOptions, options || {});

  //Create Event Emiter.
  oThis.eventEmitter = new Events.EventEmitter();
  oThis.eventEmitter.setMaxListeners(oThis.options.emitterMaxListeners);

  //Call the baseclass constructor with arguments.
  return WebsocketProvider.apply(oThis, arguments) || oThis;
});

web3-providers-ws

Websocket provider for Web3 4.x.x

LGPL-3.0
Latest version published 5 months ago

Package Health Score

96 / 100
Full package analysis

Similar packages