1
1
import React , { Component } from 'react' ;
2
- import { createEoswsSocket , EoswsClient , InboundMessageType } from '@dfuse/eosws-js ' ;
2
+ import { InboundMessageType , createDfuseClient } from '@dfuse/client ' ;
3
3
4
4
import './App.css' ;
5
5
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"
8
8
9
9
class App extends Component {
10
10
state = {
@@ -17,35 +17,36 @@ class App extends Component {
17
17
super ( )
18
18
19
19
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
+ } )
26
30
}
27
31
28
32
componentWillUnmount ( ) {
29
33
if ( this . stream !== undefined ) {
30
- this . stream . unlisten ( )
34
+ this . stream . close ( )
31
35
}
32
-
33
- // Try our best to disconnect gracefully
34
- this . client . disconnect ( )
35
36
}
36
37
37
38
launch = async ( ) => {
38
- if ( ! apiToken ) {
39
+ if ( ! apiKey ) {
39
40
const messages = [
40
41
"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." ,
42
43
"" ,
43
44
"To make it into effect, define the variable before starting the development" ,
44
45
"scripts, something like:" ,
45
46
"" ,
46
- "REACT_APP_DFUSE_API_TOKEN=ey ....af yarn start " ,
47
+ "REACT_APP_DFUSE_API_KEY=web_ ...." ,
47
48
"" ,
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"
49
50
]
50
51
51
52
this . setState ( { connected : false , errorMessages : messages , transfers : [ ] } )
@@ -55,20 +56,16 @@ class App extends Component {
55
56
this . setState ( { errorMessages : [ ] , transfers : [ ] } )
56
57
57
58
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 )
60
62
61
- this . streamTransfer ( )
63
+ this . setState ( { connected : true } )
62
64
} catch ( error ) {
63
65
this . setState ( { errorMessages : [ "Unable to connect to socket." , JSON . stringify ( error ) ] } )
64
66
}
65
67
}
66
68
67
- streamTransfer = ( ) => {
68
- this . stream = this . client . getActionTraces ( { account : "eosio.token" , action_name : "transfer" } )
69
- this . stream . onMessage ( this . onMessage )
70
- }
71
-
72
69
onMessage = async ( message ) => {
73
70
if ( message . type !== InboundMessageType . ACTION_TRACE ) {
74
71
return
@@ -87,11 +84,9 @@ class App extends Component {
87
84
return
88
85
}
89
86
90
- this . stream . unlisten ( )
91
- this . stream = undefined
92
-
93
87
try {
94
- await this . client . disconnect ( )
88
+ await this . stream . close ( )
89
+ this . stream = undefined
95
90
} catch ( error ) {
96
91
this . setState ( { errorMessages : [ "Unable to disconnect socket correctly." , JSON . stringify ( error ) ] } )
97
92
}
@@ -105,11 +100,6 @@ class App extends Component {
105
100
this . setState ( { errorMessages : [ "An error occurred with the socket." , JSON . stringify ( error ) ] } )
106
101
}
107
102
108
- onReconnect = ( ) => {
109
- this . setState ( { connected : true } )
110
- this . streamTransfer ( )
111
- }
112
-
113
103
renderTransfer = ( transfer , index ) => {
114
104
return < code key = { index } className = "App-transfer" > { transfer } </ code >
115
105
}
0 commit comments