Client for Iroha 2, which is used to submit requests to Iroha peer.
This version of package targets Iroha 2.0.0-pre-rc.20
.
The packages are published under the @iroha2
scope into Iroha Nexus Registry.
To install client
with npm
/pnpm
:
-
Configure your package manager to fetch scoped packages from Nexus Registry.
# FILE: .npmrc @iroha2:registry=https://nexus.iroha.tech/repository/npm-group/
-
Install the
client
package:npm i @iroha2/client
Iroha Client uses WebSocket and HTTP to communicate with an Iroha Peer. There is no way for Iroha Client to communicate with a peer in an environment-agnostic way.
Previously, @iroha2/client
used @iroha2/client-isomorphic-ws
and @iroha2/client-isomorphic-fetch
to seamlessly switch between environment-specific transports. Due to Node.js moving towards ESM, the way isomorphism used to be achieved is no longer applicable, and these packages are no longer in use.
Now the client provides additional entrypoints for isomorphic adapters, the same way it is done in isomorphic-git. You need to provide fetch
/ws
yourself.
For WebSocket, the client has two entrypoints: @iroha2/client/web-socket/native
and @iroha2/client/web-socket/node
.
-
Import adapters:
Import
@iroha2/client/web-socket/native
for when the nativeWebSocket
exists:import { adapter } from '@iroha2/client/web-socket/native'
Import
@iroha2/client/web-socket/node
with thews
package (it is a peer dependency of the client package) for Node.js:// you need to install `ws` package first npm i ws @types/ws
import { adapter } from '@iroha2/client/web-socket/node'
-
Use WebSocket adapter with Torii:
import { Torii } from '@iroha2/client' Torii.listenForEvents({ adapter })
fetch
could be provided in the same way. However, @iroha2/client
does not provide it itself. There are node-fetch
and undici
packages that provide the fetch
implementation that could be injected into Iroha Client.
-
Import fetch from
node-fetch
orundici
packages:import nodeFetch from 'node-fetch' import { fetch as undiciFetch } from 'undici'
-
Inject
fetch
into Iroha Client:import { Torii } from '@iroha2/client' Torii.getStatus({ fetch: nodeFetch as typeof fetch })
In Browser:
import { Torii } from '@iroha2/client' Torii.getStatus({ fetch: fetch.bind(window) })
Note: we make
fetch.bind(window)
to avoidTypeError: "'fetch' called on an object that does not implement interface Window."
.
Refer to Iroha 2 tutorial for instructions on how to configure the client.