-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathpolygon-business.js
29 lines (22 loc) · 1.12 KB
/
polygon-business.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
import { websocketClient } from "@polygon.io/client-js";
/*
This example uses polygon client-js library to connect to the polygon stocks for business
websocket to subscribe to the fair market value channel for the ticker AAPL.
*/
// create a websocket client using the polygon client-js library
const ws = websocketClient('API KEY', 'wss://business.polygon.io').stocks();
// register a handler to log errors
ws.onerror = (err) => console.log('Failed to connect', err);
// register a handler to log info if websocket closes
ws.onclose = (code, reason) => console.log('Connection closed', code, reason);
// register a handler when messages are received
ws.onmessage = (msg) => {
// parse the data from the message
const parsedMessage = JSON.parse(msg.data);
// wait until the message saying authentication was successful, then subscribe to a channel
if (parsedMessage[0].ev === 'status' && parsedMessage[0].status === 'auth_success') {
console.log('Subscribing to Fair Market Value channel for ticker AAPL');
ws.send(JSON.stringify({"action":"subscribe", "params":"FMV.AAPL"}));
}
console.log('Message received:', parsedMessage);
}