-
Notifications
You must be signed in to change notification settings - Fork 28
/
forever-streaming.ts
62 lines (53 loc) · 1.97 KB
/
forever-streaming.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
import { DFUSE_API_KEY, runMain, DFUSE_API_NETWORK } from "../../config"
import { createDfuseClient, InboundMessage, waitFor, Stream } from "@dfuse/client"
/**
* In this example, we showcase how the library always keeps your
* streams active. By default, the library will automatically restart all
* your active streams after a reconnection event occurred on the
* underlying socket.
*
* With zero effort from your part, your stream is always receiving
* messages. This pattern can be used when you only really need a never ending
* streaming of messages, whithout caring about any messages that may have
* missed while being disconnected from the socket.
*
* It's possible to deactivate this behavior by using the [[StreamClientOptions]]
* `autoRestartStreamsOnReconnect` and set it to `false`.
*
* **Important**
* If it's really important to never miss a single message, you will need to also mark
* progress to ensure you reconnect at the right moment. Look at the `never-miss-a-beat.ts`
* example that showcases how to implement a bulletproof data integrity pattern and ensure
* you never miss or skip an important message by mistake.
*/
async function main(): Promise<void> {
const client = createDfuseClient({
apiKey: DFUSE_API_KEY,
network: DFUSE_API_NETWORK,
})
const stream: Stream = await client.streamActionTraces(
{
accounts: "eosio.token",
action_names: "create",
},
onMessage
)
stream.onPostRestart = () => {
console.log("Socket reconnected, your stream(s) have restarted automatically!")
}
console.log("Socket is now connected.")
await waitFor(38000)
await stream.close()
client.release()
}
function onMessage(message: InboundMessage): void {
if (message.type === "listening") {
// You should see this message a second time when restart of stream occurs
console.log("Stream is now listening.")
return
}
if (message.type === "action_trace") {
console.log("Streaming transfer.")
}
}
runMain(main)