npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dfuse/eosws-js

v0.11.12

Published

`eosws` JavaScript/TypeScript bindings (from the [dfuse API](https://dfuse.io/))

Downloads

5

Readme

eosws JavaScript/TypeScript bindings (from the dfuse API)

WebSocket consumer for the https://dfuse.io API on EOS networks.

Deprecated

IMPORTANT This library is now deprecated and is not actively maintained anymore.

We transform it into a full blown dfuse API Client Library. We highly recommend to upgrade, to profit from automatic authentication handling, full dfuse REST API, automatic re-connect and stream restart, streamlined overall usage of the library and all the latest goodies of dfuse API. We have prepared an extensive Migration Guide so you can migrate from this library to the latest version like a breeze.

The development now occurs at https://github.com/dfuse-io/client-js.

Installation

Using Yarn:

yarn add @dfuse/eosws-js

or using NPM:

npm install --save @dfuse/eosws-js

Quick Start

When targeting a browser (you will need a bundler like Webpack since we only ship ES5 modules files for now):

const { EoswsClient, createEoswsSocket, InboundMessageType } = require("@dfuse/eosws-js")

const endpoint = "mainnet.eos.dfuse.io"
const token = "<Paste your API token here>"
const client = new EoswsClient(
  createEoswsSocket(() => new WebSocket(`wss://${endpoint}/v1/stream?token=${token}`))
)

client
  .connect()
  .then(() => {
    client
      .getActionTraces({ account: "eosio.token", action_name: "transfer" })
      .onMessage((message) => {
        if (message.type === InboundMessageType.ACTION_TRACE) {
          const { from, to, quantity, memo } = message.data.trace.act.data
          console.log(from, to, quantity, memo)
        }
      })
  })
  .catch((error) => {
    console.log("Unable to connect to dfuse endpoint.", error)
  })

Use Cases

You can see various examples in the examples folder. Here the reference list:

Endpoints

Here are the currently available endpoints:

  • Mainnet mainnet.eos.dfuse.io
  • Jungle jungle.eos.dfuse.io
  • Kylin kylin.eos.dfuse.io

Node.js

If you target a Node.js environment instead, import a proper WebSocket client implementation.

For now, the library only accepts a WebSocket that follows the WebSocket Web API interface. We will provide adapter for the most used packages in the near feature.

For now, the preferred method in Node.js is to use the ws. This package is compatible with the WebSocket Web API.

Package ws

Ensure the you added ws as a dependency of your project. Then, do the following:

const WebSocket = require("ws")

// ... rest as the browser example above

Package websocket

The client of this package does not follow WebSocket Web API specification. As such, it's currently not supported by this library.

We might provide an adapter in the future, but nothing is planned yet, pull request welcome!

Note It can be used standalone (i.e. without this library), if you really need it, but won't be able to leverage all the goodies this library provides for you.

API

Protocol

For the low-level communication protocol, heads down to dfuse WebSocket API Documentation.

Library

Here the API reference for this actual library.

SocketFactory

A type representing a function taking no parameters and that must return a WebSocket object that will be used to connect to the remote endpoint.

Example
const socketFactory: SocketFactory = () =>
    new WebSocket(`wss://${endpoint}/v1/stream?token=${token}`, { origin })))

createEoswsSocket

A factory method responsible of creating a EoswsSocket object that is consumed by the EoswsClient.

Parameters
  • socketFactory SocketFactory The factory function that will be used to construct the underlying socket.
  • options SocketOptions The options object to pass to the EoswsSocket interface.

SocketOptions

Properties
  • id string An id to identify the actual socket for debugging purposes. (Optional, undefined by default)
  • keepAlive boolean Determine if if a pong message should be sent at regular interval to ensure the connection is healthy. (Optional, true by default).
  • keepAliveIntervalInMs number The amount of time in milliseconds to wait between each keep alive message sending. (Optional, 30000 (30s) by default).
  • autoReconnect boolean Determine if the socket should auto-reconnect to the remote endpoint when connection is closed abnormally. (Optional, true by default).
  • reconnectDelayInMs number The amount of time in milliseconds to wait before trying a second reconnection attempt. (Optional, 5000 (5s) by default).
  • onInvalidMessage (message: object) => void A function that is invoked back when a message of an unknown type is returned to the client. (Optional, undefined by default)
  • onReconnect () => void A function that is invoked back when a successful reconnection happen. (Optional, undefined by default)
  • onError (event: object) => void A function that is invoked back when socket receive an ErrorEvent according to the WebSocket protocol. (Optional, undefined by default)
  • onClose (event: object) => void A function that is invoked back when socket receives a CloseEvent according to the WebSocket protocol. (Optional, undefined by default)

EoswsClient

A class handling the communication with the remote endpoint dealing with all the details of the Eosws protocol.

constructor

Constructs an EoswsClient instance.

Parameters
connect
Parameters

None

Return

Returns a Promise<void> resolving correctly once connected initially on the remote endpoint.

disconnect

Returns a Promise<void> resolving correctly once disconnection has completed with the client.

getActionTraces
Parameters
  • data GetActionTracesData Data Parameters for receiving action traces.
    • account string Contract account targeted by the action.
    • receiver string? Specify the receiving account executing its smart contract. If left blank, defaults to the same value as account.
    • action_name string? Name of the action called within the account contract.
    • with_ramops boolean? Stream RAM billing changes and reasons for costs of storage produced by each action.
    • with_inline_traces boolean? Stream the inline actions produced by each action.
    • with_deferred boolean? Stream the modifications to deferred transactions produced by each action.
  • options StreamOptions Optional common stream options (optional, default {})
Return

A ListenerObject on which you can start listening for message related to the stream by calling listen on it and stop listening by calling unlisten on it.

getTableRows
Parameters
  • data GetTableRows Data Parameters
    • code string Contract account which wrote to tables.
    • scope string Table scope where table is stored.
    • table string Table name, shown in the contract ABI.
    • json boolean With json=true (or 1), table rows will be decoded to JSON, using the ABIs active on the queried block. This endpoint will thus automatically adapt to upgrades to the ABIs on chain. (optional, default true)
  • options StreamOptions Optional common stream options (optional, default {})
Return

A ListenerObject on which you can start listening for message related to the stream by calling listen on it and stop listening by calling unlisten on it.

getTransactionLifecycle
Parameters
  • id string The transaction id to get transaction info.
  • options StreamOptions Optional common stream options (optional, default {})
Return

A ListenerObject on which you can start listening for message related to the stream by calling listen on it and stop listening by calling unlisten on it.

ListenerObject

The object returned when calling one of the main stream handler so that you can then listen and unlisten on the stream.

Properties
  • listen (listener: (message: InboundMessage) => void) A function that when called, send the starting message to the server and route back all specific stream message for this request back to the listener parameter of the project.
  • reqId string The request id used to map back messages from socket to this specific stream.
  • unlisten () A function that when called, stop listening from the stream. The unlisten message is sent to the remote endpoint to stop it.

StreamOptions

An object containing the various common properties for the Eosws base messaging system.

Properties
  • req_id string An ID that you want sent back to you for any responses related to this request.
  • start_block number Block at which you want to start processing. It can be an absolute block number, or a negative value, meaning how many blocks from the current head block on the chain. Ex: -2500 means 2500 blocks in the past, relative to the head block.
  • listen boolean Whether to listen for new events upcoming for this type of stream.
  • fetch boolean Whether to fetch an initial snapshot of the requested entity.
  • with_progress number Frequency of the progress of blocks processing (within the scope of a req_id). You will, at a maximum, receive one notification each 250 milliseconds (when processing large amounts of blocks), and when blockNum % frequency == 0. When you receive a progress notification associated with a stream (again, identified by its req_id), you are guaranteed to have seen all messages produced by that stream, between the previous progress notification and the one received (inclusively).

Development

The best way to develop this library is through modifying and adding examples to the project.

To run the examples, it's quite simple, follow these instructions:

  1. Install project dependencies so that you get development tools at the same time:

    yarn install
  2. Link the project inside itself, that will be necessary to correct run the examples which import @dfuse/eosws-js:

    yarn link
    yarn link @dfuse/eosws-js
  3. Start the build watcher so distribution files are always up-to-date. Forgetting to do that will prevent examples from picking latest changes you've made to source files!

    yarn build:watch
  4. Last step is to add .env file containing the dfuse API key required to run the examples. Create a file .env at the root of the project with the following content:

    DFUSE_IO_API_KEY=Replace this with API key!
  5. Final check, let's run an example to ensure everything is working:

    yarn run ts-node examples/get-action-traces.ts

Publishing

First, ensure you have a pristine state of your working directory, and check tests & compilation:

rm -rf dist
yarn build
yarn test

Assuming you have been granted access rights to publish this package, the command to perform is simply:

yarn publish --access public

Pre-release

If you want to publish a pre-release version not flagged as the latest so that people still pulls the current stable version unless they opt-in explicitly, use the following invocation:

yarn publish --access public --tag next

Credits / Acknowledgement

A big thanks (and hug) to our dear friend Denis Carriere from EOS Nation for creating the initial version of this project.

License

MIT