-
Notifications
You must be signed in to change notification settings - Fork 59
/
ws-library-simple.js
47 lines (37 loc) · 1.33 KB
/
ws-library-simple.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
/*
This example uses the ws (https://github.com/websockets/ws) library to connect to the delayed stocks polygon
websocket to subscribe to minute ohlc values for the ticker AAPL.
*/
import WebSocket from 'ws';
const APIKEY = process.env.POLY_API_KEY || 'YOUR_API_KEY'
// Connection Type:
const ws = new WebSocket('wss://delayed.polygon.io/stocks') // stocks 15-min delay
//const ws = new WebSocket('wss://socket.polygon.io/stocks') // stocks real-time
//const ws = new WebSocket('wss://socket.polygon.io/forex') // forex
// Connection Opened:
ws.on('open', () => {
console.log('Connected!')
ws.send(`{"action":"auth","params":"${APIKEY}"}`)
// forex
//ws.send(`{"action":"subscribe","params":"C.AUD/USD,C.USD/EUR,C.USD/JPY"}`)
// aggregates
ws.send(`{"action":"subscribe","params":"AM.*"}`) // min
// ws.send(`{"action":"subscribe","params":"A.*"}`) // sec
// trades
//ws.send(`{"action":"subscribe","params":"T.*"}`)
//ws.send(`{"action":"subscribe","params":"T.TSLA"}`)
// quotes
//ws.send(`{"action":"subscribe","params":"Q.*"}`)
//ws.send(`{"action":"subscribe","params":"Q.TSLA"}`)
})
// Per message packet:
ws.on('message', ( data ) => {
data = JSON.parse( data )
data.map(( msg ) => {
if( msg.ev === 'status' ){
return console.log('Status Update:', msg.message)
}
console.log(msg)
})
})
ws.on('error', console.log)