-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkonecty-changes.js
129 lines (116 loc) · 3.17 KB
/
konecty-changes.js
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
const WebSocket = require('ws');
const EJSON = require('ejson');
const url = require('url');
module.exports = function(RED) {
function KonectyChangesNode(config) {
RED.nodes.createNode(this, config);
const node = this;
node.server = RED.nodes.getNode(config.server);
if (node.server == null) {
node.status({ fill: 'red', shape: 'ring', text: 'konecty-changes.errors.invalid-data' });
return;
}
const { host: configHost, key } = node.server;
try {
const { protocol, host } = url.parse(configHost);
const useSsl = /^https/i.test(protocol);
const endpoint = `${useSsl ? 'wss://' : 'ws://'}${host}/websocket`;
const startListening = () => {
try {
let ws = new WebSocket(endpoint);
const wsSend = message => {
ws.send(EJSON.stringify(message));
};
const doLogin = () => {
const loginMessage = {
msg: 'method',
method: 'auth:loginWithToken',
params: [{ resume: key }],
id: '1'
};
wsSend(loginMessage);
};
const subscribeChanges = () => {
wsSend({
msg: 'sub',
id: '2',
name: 'changeStream',
params: [config.document]
});
};
const heartbeat = () => {
clearTimeout(ws.pingTimeout);
ws.pingTimeout = setTimeout(() => {
node.warn(RED._('konecty-changes.errors.connection-broken'));
ws.terminate();
}, 30000 + 1000);
};
ws.on('open', () => {
heartbeat();
wsSend({
msg: 'connect',
version: '1',
support: ['1']
});
});
ws.on('close', () => {
// try to reconect in 10 seconds
node.warn(RED._('konecty-changes.errors.connection-broken'));
ws.terminate();
setTimeout(startListening, 10000);
});
ws.on('message', data => {
const { id, msg, error, collection, fields } = EJSON.parse(data);
switch (msg) {
case 'connected':
doLogin();
break;
case 'ping':
wsSend({ msg: 'pong' });
heartbeat();
break;
case 'result':
if (id === '1') {
// Login
if (error != null) {
node.error(RED._('konecty-changes.errors.error-processing', error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('konecty-changes.errors.error-processing', error)
});
} else {
subscribeChanges();
}
}
break;
case 'added':
if (fields != null && collection === config.document) {
const { type, document } = fields;
node.send({
type,
payload: document
});
}
break;
default:
break;
}
});
} catch (error) {
node.error(RED._('konecty-changes.errors.error-processing', error));
node.status({
fill: 'red',
shape: 'ring',
text: RED._('konecty-changes.errors.error-processing', error)
});
}
};
startListening();
} catch (error) {
node.status({ fill: 'red', shape: 'ring', text: RED._('konecty-changes.errors.error-processing', error) });
node.error(error);
}
}
RED.nodes.registerType('konecty-changes', KonectyChangesNode);
};