-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathws-library-advanced.js
91 lines (77 loc) · 2.3 KB
/
ws-library-advanced.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
import lodash from 'lodash';
import WebSocket from 'ws';
import { EventEmitter } from 'events';
const APIKEY = process.env.POLY_API_KEY || 'YOUR_API_KEY'
/*
This example also uses the ws (https://github.com/websockets/ws) library to connect to a websocket,
adding reconnection logic, as well as an easier
way to process each type of messages. We can use the `subscribe`
method at any time to subscribe to more channels..
*/
class Polygon extends EventEmitter {
constructor( params ){
super()
console.log('Polygon class initialized..')
this.ws = null
this.subscriptions = []
this.apiKey = params.apiKey
this.connect()
}
subscribe( channels ){
// Add to our list of subscriptions:
this.subscriptions.push( channels )
this.subscriptions = lodash.flatten( this.subscriptions )
// If these are additional subscriptions, only send the new ones:
if( this.connected ) this.sendSubscriptions( channels )
}
connect(){
this.connected = false
this.ws = new WebSocket('wss://socket.polygon.io/crypto')
this.ws.on('open', this.onOpen.bind( this ))
this.ws.on('close', this.onDisconnect.bind( this ))
this.ws.on('disconnect', this.onDisconnect.bind( this ))
this.ws.on('error', this.onError.bind( this ))
this.ws.on('message', this.onMessage.bind( this ))
}
onOpen(){
// Authenticate:
this.ws.send(`{"action":"auth","params":"${APIKEY}"}`)
this.connected = true
// Subscribe to Crypto Trades and SIP:
this.sendSubscriptions( this.subscriptions )
}
sendSubscriptions( subscriptions ){
this.ws.send(`{"action":"subscribe","params":"${subscriptions.join(',')}"}`)
}
onDisconnect(){
setTimeout( this.connect.bind( this ), 2000 )
}
onError( e ){
console.log('Error:', e)
}
onMessage( data ){
data = JSON.parse( data )
data.forEach(( msg ) => {
if( msg.ev === 'status' ){
console.log('Status Update:', msg.message)
}
this.emit(msg.ev, msg)
})
}
}
// Use our Polygon Class:
const client = new Polygon({ apiKey: APIKEY })
// Subscribe to trades:
client.subscribe([ 'XT.*' ])
// Wait 5sec, then subscribe to SIP:
setTimeout(() => {
client.subscribe(['XS.*'])
}, 5000)
// When we receive a crypto trade:
client.on('XT', ( trade ) => {
console.log('Trade:', trade)
})
// When we receive a crypto quote:
client.on('XS', ( quote ) => {
console.log('Quote:', quote)
})