forked from cryptocode/nano-websocket-sample-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
33 lines (28 loc) · 931 Bytes
/
index.js
File metadata and controls
33 lines (28 loc) · 931 Bytes
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
const WS = require('ws');
const ReconnectingWebSocket = require('reconnecting-websocket');
// Create a reconnecting WebSocket.
// In this example, we wait a maximum of 2 seconds before retrying.
const ws = new ReconnectingWebSocket('ws://[::1]:7078', [], {
WebSocket: WS,
connectionTimeout: 1000,
maxRetries: 100000,
maxReconnectionDelay: 2000,
minReconnectionDelay: 10 // if not set, initial connection will take a few seconds by default
});
// As soon as we connect, subscribe to block confirmations
ws.onopen = () => {
const confirmation_subscription = {
"action": "subscribe",
"topic": "confirmation"
}
ws.send(JSON.stringify(confirmation_subscription));
// Other subscriptions can go here
};
// The node sent us a message
ws.onmessage = msg => {
console.log(msg.data);
data_json = JSON.parse(msg.data);
if (data_json.topic === "confirmation") {
console.log ('Confirmed', data_json.message.hash)
}
};