Basic Usage
Import package
import DXClient from '@dxfeed/widgets-api'
Configure & Create connection
Create instance of DXClient and set web socket address and open connection.
import DXClient from '@dxfeed/widgets-api'const client = new DXClient('wss://CLIENT_NAME.dxfeed.com/api/rsocket')
Configure & Create subscription
You should specify the record and the requested symbols.
import { QuoteSubscription } from '@dxfeed/widgets-api/records/Quote'const subscription = await client.subscribe(new QuoteSubscription(['AAPL'] /* symbols */))
The full list of records can be found here:
import { /* Subscription */ } from '@dxfeed/widgets-api/records/*'
You can specify fields you want to request for each type of event. This can be achieved by passing a list of them as a second parameter of Subscription. If no value provided (i.e. parameter is undefined), a default set of fields will be requested. List of event types with all available fields for each of them can be found here.
client.subscribe(new QuoteSubscription(symbols,['eventSymbol', 'bidPrice'] /* fields */))
NOTE: eventSymbol field is mandatory for all types of events. If you want to request a subset of fields, you must include eventSymbol.
Some of the events may may be requested using timed subscription (i.e. with inclusion of historical data). These are:
CandleGreeksTheoPriceTimeAndSaleUnderlying
For timed subscription you should also provide time to start subscription from.
client.subscribe(new CandleSubscription(symbols,fields,1500000000000 /* from time */))
NOTE: for timed events requested with custom selection of fields, an eventFlags field is also mandatory.
/* Timed subscription, requesting a default set of fields */client.subscribe(new CandleSubscription(symbols,undefined,1500000000000))/* Timed subscription, requesting a custom set of fields */client.subscribe(new CandleSubscription(symbols,['eventSymbol', 'eventFlags', 'high', 'low'],1500000000000))
Change symbols in subscription
Add symbols
subscription.addSymbols(['MSFT'])
Remove symbols
subscription.removeSymbols(['AAPL'])
Get events from stream
After an open subscription, you can listen to events from the stream.
const stream = subscription.getStream()stream.subscribe({onSubscribe: sub => {sub.request(Number.MAX_SAFE_INTEGER) // set backpressure},onNext: handleEvents,})
onNext its event handler for process incoming events.
import { Quote } from '@dxfeed/widgets-api/records/Quote'const handleEvents = (events: Quote[]) => {/* process events */}
Close subscription stream
All onSubscribe callbacks that are called with a subscription, you need to call the subs.close method to close the stream.
stream.subscribe({onSubscribe: sub => {setTimeout(() => sub.close(), 5000) // close after 5s},onNext: console.log,})
Close connection
If you need to close the web socket connection
client.close()