Skip to content

Commit 46d8df7

Browse files
author
Matthieu Vachon
committed
Converted example to using @dfuse/client
1 parent dfffc6b commit 46d8df7

File tree

4 files changed

+33
-43
lines changed

4 files changed

+33
-43
lines changed

Diff for: README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
## dfuse Client Library - React Example
22

33
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app) and
4-
showcase how to use [dfuse Client Library](https://github.com/dfuse-io/eosws-js) to easily stream
4+
showcase how to use [dfuse Client Library](https://github.com/dfuse-io/client-js) to easily stream
55
all transfers happening in Mainnet in a streaming fashion.
66

77
First install all the dependencies:
@@ -10,14 +10,14 @@ First install all the dependencies:
1010

1111
Then simply launch the development mode to see the end results:
1212

13-
REACT_APP_DFUSE_API_TOKEN=<your dfuse API token here> yarn start
13+
REACT_APP_DFUSE_API_KEY=<dfuse API key here> yarn start
1414

1515
When running this, a browser should automatically open pointing
1616
to the example. If it's not the case, simply open http://localhost:3000 in a browser.
1717

1818
### Requirements
1919

20-
You will need to have a `REACT_APP_DFUSE_API_TOKEN` environment variable defined
20+
You will need to have a `REACT_APP_DFUSE_API_KEY` environment variable defined
2121
before starting the development server.
2222

23-
Obtain a free API token by visiting https://dfuse.io.
23+
Obtain a free API key by visiting https://dfuse.io.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"license": "MIT",
55
"private": true,
66
"dependencies": {
7-
"@dfuse/eosws-js": "^0.11.11",
7+
"@dfuse/client": "^0.2.0-rc.4",
88
"react": "^16.8.5",
99
"react-dom": "^16.8.5",
1010
"react-scripts": "2.1.8"

Diff for: src/App.js

+24-34
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { Component } from 'react';
2-
import { createEoswsSocket, EoswsClient, InboundMessageType } from '@dfuse/eosws-js';
2+
import { InboundMessageType, createDfuseClient } from '@dfuse/client';
33

44
import './App.css';
55

6-
const apiToken = process.env.REACT_APP_DFUSE_API_TOKEN
7-
const wsUrl = `wss://mainnet.eos.dfuse.io/v1/stream?token=${apiToken}`
6+
const apiKey = process.env.REACT_APP_DFUSE_API_KEY
7+
const network = process.env.REACT_APP_DFUSE_NETWORK || "mainnet"
88

99
class App extends Component {
1010
state = {
@@ -17,35 +17,36 @@ class App extends Component {
1717
super()
1818

1919
this.stream = undefined
20-
this.client = new EoswsClient(createEoswsSocket(() => new WebSocket(wsUrl), {
21-
reconnectDelayInMs: 500,
22-
onClose: this.onClose,
23-
onError: this.onError,
24-
onReconnect: this.onReconnect,
25-
}))
20+
this.client = createDfuseClient({
21+
apiKey,
22+
network,
23+
streamClientOptions: {
24+
socketOptions: {
25+
onClose: this.onClose,
26+
onError: this.onError,
27+
}
28+
}
29+
})
2630
}
2731

2832
componentWillUnmount() {
2933
if (this.stream !== undefined) {
30-
this.stream.unlisten()
34+
this.stream.close()
3135
}
32-
33-
// Try our best to disconnect gracefully
34-
this.client.disconnect()
3536
}
3637

3738
launch = async () => {
38-
if (!apiToken) {
39+
if (!apiKey) {
3940
const messages = [
4041
"To correctly run this sample, you need to defined an environment variable",
41-
"named 'REACT_APP_DFUSE_API_TOKEN' with the value being your dfuse API token.",
42+
"named 'REACT_APP_DFUSE_API_KEY' with the value being your dfuse API token.",
4243
"",
4344
"To make it into effect, define the variable before starting the development",
4445
"scripts, something like:",
4546
"",
46-
"REACT_APP_DFUSE_API_TOKEN=ey....af yarn start",
47+
"REACT_APP_DFUSE_API_KEY=web_....",
4748
"",
48-
"You can obtain a free API token by visiting https://dfuse.io"
49+
"You can obtain a free API key by visiting https://dfuse.io"
4950
]
5051

5152
this.setState({ connected: false, errorMessages: messages, transfers: [] })
@@ -55,20 +56,16 @@ class App extends Component {
5556
this.setState({ errorMessages: [], transfers: [] })
5657

5758
try {
58-
await this.client.connect()
59-
this.setState({ connected: true })
59+
this.stream = await this.client.streamActionTraces({
60+
account: "eosio.token", action_name: "transfer"
61+
}, this.onMessage)
6062

61-
this.streamTransfer()
63+
this.setState({ connected: true })
6264
} catch (error) {
6365
this.setState({ errorMessages: ["Unable to connect to socket.", JSON.stringify(error)] })
6466
}
6567
}
6668

67-
streamTransfer = () => {
68-
this.stream = this.client.getActionTraces({ account: "eosio.token", action_name: "transfer" })
69-
this.stream.onMessage(this.onMessage)
70-
}
71-
7269
onMessage = async (message) => {
7370
if (message.type !== InboundMessageType.ACTION_TRACE) {
7471
return
@@ -87,11 +84,9 @@ class App extends Component {
8784
return
8885
}
8986

90-
this.stream.unlisten()
91-
this.stream = undefined
92-
9387
try {
94-
await this.client.disconnect()
88+
await this.stream.close()
89+
this.stream = undefined
9590
} catch (error) {
9691
this.setState({ errorMessages: ["Unable to disconnect socket correctly.", JSON.stringify(error)]})
9792
}
@@ -105,11 +100,6 @@ class App extends Component {
105100
this.setState({ errorMessages: ["An error occurred with the socket.", JSON.stringify(error)]})
106101
}
107102

108-
onReconnect = () => {
109-
this.setState({ connected: true })
110-
this.streamTransfer()
111-
}
112-
113103
renderTransfer = (transfer, index) => {
114104
return <code key={index} className="App-transfer">{transfer}</code>
115105
}

Diff for: yarn.lock

+4-4
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,10 @@
10411041
resolved "https://registry.yarnpkg.com/@csstools/convert-colors/-/convert-colors-1.4.0.tgz#ad495dc41b12e75d588c6db8b9834f08fa131eb7"
10421042
integrity sha512-5a6wqoJV/xEdbRNKVo6I4hO3VjyDq//8q2f9I6PBAvMesJHFauXDorcNCsr9RzvsZnaWi5NYCcfyqP1QeFHFbw==
10431043

1044-
"@dfuse/eosws-js@^0.11.11":
1045-
version "0.11.11"
1046-
resolved "https://registry.yarnpkg.com/@dfuse/eosws-js/-/eosws-js-0.11.11.tgz#9724bf21e4f1fe3932279f8e4eceec19a990285d"
1047-
integrity sha512-jRY4AruDTNOsvgqiG3QUYy6YyEr5YfFyYyeBMFRprkEA2cTcCev/R3OJ94lLhsnQWU/zl8UqBwaX/qWiyjSNQA==
1044+
"@dfuse/client@^0.2.0-rc.4":
1045+
version "0.2.0-rc.4"
1046+
resolved "https://registry.yarnpkg.com/@dfuse/client/-/client-0.2.0-rc.4.tgz#e62b509b71019bb02aa394af6f19d479703f2b5d"
1047+
integrity sha512-yI176XjH/1oiOfNqwloWdH/yC6QTFuCE5GQ3tW91j5goFTK0mYoC5JLASSF6oEXMJJNqUhReXwqX8Y6BL9dwZw==
10481048
dependencies:
10491049
debug "^4.1.0"
10501050

0 commit comments

Comments
 (0)