forked from dfuse-io/client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-and-socket-notifications.ts
130 lines (112 loc) · 3.87 KB
/
client-and-socket-notifications.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { DFUSE_API_KEY, runMain, DFUSE_API_NETWORK } from "../../config"
import {
createDfuseClient,
InboundMessage,
InboundMessageType,
waitFor,
SocketOptions,
GraphqlStreamMessage,
} from "@dfuse/client"
/**
* In this example, we will showcase how to be get notifications when certain
* events happen in the client and underlying socket.
*
* You probably won't see that much here, unless you are able to
* generate a closing of the connection and then allow it to come
* back. Restarting the network interface while the script is running
* might achieve this.
*
* In this example, you will register a listener for the following events:
* - Socket `onError`: when an error occurs with the connection. You will still receive an `onClose` right aftet this one.
* - Socket `onClose`: when the connection of the `Socket` was closed.
* - Socket `onReconnect`: when the socket has automatically reconnected.
*
* We will also register an `onPostRestart` listener on the `Stream`, which is called after
* a `listen` has been sent back to the remote endpoint due to a socket `onReconnect`.
*
* The example also show all cases that can happen with both streaming methods.
*/
async function main(): Promise<void> {
const socketOptions: SocketOptions = {
onError(event: any) {
console.log("Socket emitted an error event.", {
message: event.message,
error: event.error,
})
},
onClose(event: any) {
console.log("Socket has closed its connection.", { reason: event.reason, code: event.code })
},
onReconnect() {
console.log("Socket has been reconnected with remote server.")
},
}
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: DFUSE_API_NETWORK,
streamClientOptions: {
socketOptions,
},
graphqlStreamClientOptions: {
socketOptions,
},
})
const graphqlOperation = `subscription($cursor: String!) {
searchTransactionsForward(query: "action:onblock", cursor: $cursor) {
undo cursor
block { num timestamp }
}
}`
const graphqlStream = await client.graphql(
graphqlOperation,
(message: GraphqlStreamMessage<any>) => {
if (message.type === "error") {
// When `terminal: true`, an auto-reconnection is automatically performed
console.log("GraphQL stream error.", message.errors, message.terminal)
return
}
if (message.type === "data") {
console.log(
"GraphQL stream data.",
JSON.stringify({ ...message.data.searchTransactionsForward, cursor: undefined })
)
// Mark latest location where we want to start back at
graphqlStream.mark({ cursor: message.data.searchTransactionsForward.cursor })
}
if (message.type === "complete") {
console.log("GraphQL stream completed.")
}
}
)
graphqlStream.onPostRestart = () => {
console.log()
console.log(
"<============= GraphQL stream has restarted to its previous `mark()` location =============>"
)
}
const wsStream = await client.streamHeadInfo((message: InboundMessage) => {
if (message.type === InboundMessageType.ERROR) {
console.log("WebSocket stream error.", message.data)
return
}
if (message.type === InboundMessageType.LISTENING) {
console.log("WebSocket stream is now listening.")
}
if (message.type === InboundMessageType.HEAD_INFO) {
console.log("WebSocket stream data.", JSON.stringify(message.data))
// Mark latest location where we want to start back at
wsStream.mark({ atBlockNum: message.data.head_block_num })
}
})
wsStream.onPostRestart = () => {
console.log()
console.log(
"<============= WebSocket stream has restarted to its previous `mark()` location =============>"
)
}
await waitFor(35000)
await graphqlStream.close()
await wsStream.close()
client.release()
}
runMain(main)