This repository was archived by the owner on Aug 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketClient.js
More file actions
195 lines (171 loc) · 5.06 KB
/
Copy pathWebSocketClient.js
File metadata and controls
195 lines (171 loc) · 5.06 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const WebSocket = require('ws');
const EventEmitter = require('events');
const constants = require('./constants');
/**
* NOWPayments WebSocket client for real-time payment updates
* @class NOWPaymentsWebSocket
* @extends EventEmitter
* @typedef {import('../types/websocket').WebSocketOptions} WebSocketOptions
* @typedef {import('../types/websocket').PaymentUpdateEvent} PaymentUpdateEvent
* @typedef {import('../types/websocket').WebSocketError} WebSocketError
* @typedef {import('../types/websocket').ConnectionState} ConnectionState
*/
class NOWPaymentsWebSocket extends EventEmitter {
/**
* Creates WebSocket client instance
* @param {string} apiKey - NOWPayments API key
* @param {WebSocketOptions} [options] - Client configuration options
*/
constructor(apiKey, options = {}) {
super();
this.apiKey = apiKey;
this.options = {
maxReconnectAttempts: options.maxReconnectAttempts || 5,
reconnectDelay: options.reconnectDelay || 1000,
pingInterval: options.pingInterval || 30000,
pongTimeout: options.pongTimeout || 5000,
endpoint: options.endpoint || constants.WEBSOCKET_ENDPOINTS.PRODUCTION
};
this.reconnectAttempts = 0;
this.isConnected = false;
this.lastPing = null;
this.pongTimeout = null;
this.pingInterval = null;
this.reconnectTimer = null;
}
/**
* Establishes WebSocket connection
* @fires NOWPaymentsWebSocket#connected
* @fires NOWPaymentsWebSocket#disconnected
* @fires NOWPaymentsWebSocket#payment_update
* @fires NOWPaymentsWebSocket#error
*/
connect() {
if (this.ws) {
this.close();
}
this.ws = new WebSocket(this.options.endpoint, {
headers: {
'x-api-key': this.apiKey
},
handshakeTimeout: 10000
});
this._setupEventHandlers();
this._startPingPong();
}
/**
* Setup WebSocket event handlers
* @private
*/
_setupEventHandlers() {
this.ws.on('open', () => {
this.isConnected = true;
this.reconnectAttempts = 0;
this.emit('connected');
});
this.ws.on('message', data => {
try {
const message = JSON.parse(data);
if (message.type === 'payment_update') {
this.emit('payment_update', message);
}
} catch (error) {
this.emit('error', this._createError('MessageParseError', error.message));
}
});
this.ws.on('pong', () => {
clearTimeout(this.pongTimeout);
const latency = Date.now() - this.lastPing;
this.emit('pong', latency);
});
this.ws.on('close', (code, reason) => {
this._handleDisconnect(code, reason.toString());
});
this.ws.on('error', error => {
this.emit('error', this._createError('WebSocketError', error.message));
});
}
/**
* Starts ping/pong heartbeat
* @private
*/
_startPingPong() {
this.pingInterval = setInterval(() => {
if (this.isConnected) {
this.lastPing = Date.now();
this.ws.ping();
this.pongTimeout = setTimeout(() => {
this._handleDisconnect(1001, 'Pong timeout');
}, this.options.pongTimeout);
}
}, this.options.pingInterval);
}
/**
* Handles WebSocket disconnection
* @private
* @param {number} code - Close code
* @param {string} reason - Close reason
*/
_handleDisconnect(code, reason) {
this.isConnected = false;
clearInterval(this.pingInterval);
clearTimeout(this.pongTimeout);
this.emit('disconnected', reason);
if (this.reconnectTimer) {
clearTimeout(this.reconnectTimer);
}
if (this.reconnectAttempts < this.options.maxReconnectAttempts) {
this.reconnectAttempts++;
const delay = this.options.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
this.emit('reconnecting', this.reconnectAttempts);
this.reconnectTimer = setTimeout(() => {
this.connect();
}, delay);
} else {
this.emit(
'error',
this._createError('MaxReconnectError', 'Maximum reconnection attempts reached')
);
}
}
/**
* Creates WebSocket error object
* @private
* @param {string} code - Error code
* @param {string} message - Error message
* @returns {WebSocketError} WebSocket error
*/
_createError(code, message) {
const error = new Error(message);
error.code = code;
return error;
}
/**
* Closes WebSocket connection
*/
close() {
clearInterval(this.pingInterval);
clearTimeout(this.pongTimeout);
clearTimeout(this.reconnectTimer);
if (this.ws) {
this.ws.removeAllListeners();
if ([WebSocket.OPEN, WebSocket.CONNECTING].includes(this.ws.readyState)) {
this.ws.close(1000, 'Client closed connection');
}
this.ws = null;
}
this.isConnected = false;
}
/**
* Gets current connection state
* @returns {ConnectionState} Connection state
*/
getState() {
return {
isConnected: this.isConnected,
lastConnected: this.lastPing ? new Date(this.lastPing) : undefined,
reconnectAttempts: this.reconnectAttempts
};
}
}
module.exports = NOWPaymentsWebSocket;